0

我在 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.
4

2 回答 2

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 即使它是

解决方案

理想情况下,您需要:

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

    from selenium import webdriver
    
    driver = webdriver.Firefox(executable_path=r'/non_root_user/Downloads/geckodriver')
    driver.get("http://google.com/")
    driver.quit()
    
于 2019-08-12T09:50:04.333 回答
0

只需将 geckodriver 文件复制到您的 PATH 中即可。键入 echo $PATH 以查找您的 PATH

于 2021-01-28T09:36:21.647 回答