【问题标题】:Selenium Geckodriver and ChromeDriver not workingSelenium Geckodriver 和 ChromeDriver 不工作
【发布时间】:2018-05-29 01:59:54
【问题描述】:

使用 Python 3.6 绑定运行 OS 10.12.6 Selenium

尽管我尽了最大的努力,但我似乎无法与 Selenium 一起工作。这是我得到的错误:

Geckodriver 错误:

Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 74, in start
stdout=self.log_file, stderr=self.log_file)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "/Library/Frameworks/Python.framework/Versions/3.6/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: 'geckodriver': 'geckodriver'

在处理上述异常的过程中,又发生了一个异常:

Traceback (most recent call last):
File "/Users/christopher.gaboury/Desktop/Scripts/safariExecutive.py", line 11, in <module>
browser = webdriver.Firefox()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 148, in __init__
self.service.start()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 81, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

chromedriver 和 geckodriver 的错误基本相同。

我已经手动设置了这些路径所在的位置。同样的错误。我已将驱动程序移至路径中已存在的位置。同样的错误。我已经删除了我通过 Homebrew 下载并安装了两个驱动程序的两个版本。同样的错误。我不确定下一步该怎么做。

【问题讨论】:

  • echo $PATH,看看你的网络驱动程序是否在这些文件夹中。
  • 使用代码块
  • 我已经手动将驱动程序移动到路径中的位置,并允许 Homebrew 为我安装它们。两个实例都不起作用。
  • 这似乎更适合作为问题/错误提交给Selenium
  • which selenium 的结果是什么?

标签: python selenium selenium-chromedriver geckodriver


【解决方案1】:

我创建了新的 bash 脚本,它会自动安装 geckodriver、firefox、python、selenium 并测试配置。

#!/bin/bash

apt update && apt install -y firefox python3 python3-pip
pip install selenium
INSTALL_DIR="/usr/local/bin"
json=$(curl -s https://api.github.com/repos/mozilla/geckodriver/releases/latest)
input=$(echo "$json" | jq -r '.assets[].browser_download_url | select(contains("linux64"))')
urls=$(echo $input | tr " " "\n")
for addr in $urls
do
        url=$addr
        break
done
curl -s -L "$url" | tar -xz
chmod +x geckodriver
sudo mv geckodriver "$INSTALL_DIR"
echo "installed geckodriver binary in $INSTALL_DIR"
python3 -c "from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
driver.get(\"http://google.com/\")
print (\"Headless Firefox Initialized\")
driver.quit()"

【讨论】:

    【解决方案2】:

    我现在可以毫无问题地使用 chromedriver。我将 chromedriver.exe 设置在重命名为 chromedriver 的文件夹中,然后确保文件路径在括号中。然而,尽管 pathlib.Path 识别出 geckodriver.exe 文件存在,但我在通过 selenium 打开 geckodriver 时遇到了同样的问题。看看你是否可以让 chromedriver 以这种方式工作:

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    browser=webdriver.Chrome(r'c:\path_to_chromedriver\chromedriver\chromedriver.exe')
    

    与上面的最后一行一样,您应该确保以原始字符串的形式包含文件路径(原始字符串确保 Python 不会将反斜杠读取为转义字符)。如果您确实知道如何使用 geckodriver 解决您的问题(因为我尝试通过高级设置将其添加到系统路径但它不起作用),请告诉我,但这应该让 Selenium 与 Chrome 一起使用。

    【讨论】:

      【解决方案3】:

      Homebrew 需要链接驱动程序。完成此操作后,它们就可以完美运行了。

      【讨论】:

      • @thewaywewere - 这是 OP 发布他们对自己问题的解决方案。所以,是的,它确实为他们的具体问题提供了答案。
      • 您可以为以后可能出现的任何人添加有关如何在 Homebrew 中链接驱动程序的说明。 ...并确保您对任何有帮助的答案进行投票并接受您自己的答案,这样其他人就不会浪费时间尝试解决问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-06
      • 2018-02-09
      • 2021-07-17
      • 1970-01-01
      • 2020-11-03
      相关资源
      最近更新 更多