【问题标题】:Firefox Selenium in TermuxTermux 中的 Firefox Selenium
【发布时间】:2019-08-12 08:05:53
【问题描述】:

我在 termux 中运行 ubuntu,我安装了 pycharm 来创建 python 代码。问题是我在打开 webdriver 时遇到了一些错误。

我有最新的 firefox (v59.0.2)、Selenium geckodriver v.0.24.0 并使用 python 3.6.5

这是代码

from selenium import webdriver

driver = webdriver.Firefox('/root/Downloads/geckodriver')

这是错误

Traceback (most recent call last):
   File "<input>", line 3, in <module>
   File "/root/PycharmProjects/untitled/venv/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 151, in __init__
firefox_profile = FirefoxProfile(firefox_profile)
  File "/root/PycharmProjects/untitled/venv/lib/python3.6/site-packages/selenium/webdriver/firefox/firefox_profile.py", line 80, in __init__
    ignore=shutil.ignore_patterns("parent.lock", "lock", ".parentlock"))
  File "/usr/lib/python3.6/shutil.py", line 309, in copytree
names = os.listdir(src)
FileNotFoundError: [Errno 2] No such file or directory: '/root/Downloads/geckodriver'

如果我这样做

from selenium import webdriver

driver = webdriver.Firefox(executable_path='/root/Downloads/geckodriver')

这是错误

    Traceback (most recent call last):
    File "/root/PycharmProjects/untitled/venv/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 76, in start
    stdin=PIPE)
  File "/usr/lib/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.6/subprocess.py", line 1344, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/root/Downloads/geckodriver': '/root/Downloads/geckodriver'
During handling of the above exception, another exception occurred:
    Traceback (most recent call last):
      File "<input>", line 3, in <module>
  File "/root/PycharmProjects/untitled/venv/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 164, in __init__
    self.service.start()
  File "/root/PycharmProjects/untitled/venv/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 83, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

【问题讨论】:

  • 如果你尝试绝对路径会发生什么? executable_path='C:/path_to_root/root/Downloads/geckodriver
  • 我相信C盘只适用于windows,我在termux bud中使用ubuntu。
  • 驱动器是什么并不重要,这只是一个例子。
  • 好吧,鉴于我真的不知道这些东西,该怎么做?我的意思是我不知道用path_to_root 替换什么。我唯一知道的是我的文件系统以 /root/ 开头

标签: python-3.x selenium firefox geckodriver termux


【解决方案1】:

根据您的第一次代码试用,此错误消息...

FileNotFoundError: [Errno 2] No such file or directory: '/root/Downloads/geckodriver'

...表示您的程序无法在上述目录中找到 GeckoDriver

您可以在讨论FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver': 'geckodriver' with GeckoDriver and Python in MAC OS中找到对此错误的详细分析

根据您的第二次代码试用此错误消息...

selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

...表示您的程序无法在上述目录中找到 GeckoDriver

您可以在讨论WebDriverException: 'geckodriver' executable needs to be in PATH even though it is中找到对此错误的详细分析

解决方案

理想情况下,您需要:

  • 确保 GeckoDriver非 root 用户具有 executable 权限。
  • 非root用户身份执行@Tests
  • 使用以下代码块:

    from selenium import webdriver
    
    driver = webdriver.Firefox(executable_path=r'/non_root_user/Downloads/geckodriver')
    driver.get("http://google.com/")
    driver.quit()
    

【讨论】:

  • 对于项目符号 1 和 3,我有 termux,它是 virtualenv,所以它唯一的 root 用户不是,除非我有办法检查 非 root 用户,我检查了权限及其所有 read and write ,我还尝试将其放入/usr/local/bin,结果为unexpectedly exited. Status code was: -4,对于子弹2,我不知道,我是新手
【解决方案2】:

只需将 geckodriver 文件复制到您的 PATH 中即可。 类型 回声 $PATH 找到你的路径

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2015-04-04
    • 2021-06-08
    • 1970-01-01
    相关资源
    最近更新 更多