【问题标题】:Unable to open Firefox browser using Selenium无法使用 Selenium 打开 Firefox 浏览器
【发布时间】:2017-08-01 15:38:17
【问题描述】:

当我尝试使用 python 脚本在 Ubuntu Linux 14.04 上打开 firefox 浏览器时收到此错误消息:

File "seleniumtest.py", line 3, in <module>
browser = webdriver.Firefox()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 145, in __init__
self.service.start()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/common/service.py", line 74, in start
stdout=self.log_file, stderr=self.log_file)
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 8] Exec format error

示例代码为:

from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://www.google.com')

geckodriver 等依赖已安装。

wget https://github.com/mozilla/geckodriver/releases/download/v0.13.0/geckodriver-v0.13.0-linux64.tar.gz
tar -xvzf geckodriver-v0.13.0-linux64.tar.gz
rm geckodriver-v0.13.0-linux64.tar.gz
chmod +x geckodriver
cp geckodriver /usr/local/bin/

加上"executable_path="/usr/local/bin/geckodriver",输出如下:

File "seleniumtest.py", line 3, in <module>
browser = webdriver.Firefox(executable_path="/usr/local/bin/geckodriver")
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 145, in __init__
self.service.start()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/common/service.py", line 74, in start
stdout=self.log_file, stderr=self.log_file)
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 8] Exec format error

【问题讨论】:

  • 可能重复:stackoverflow.com/questions/38833589/…stackoverflow.com/questions/36141032/… 不同的操作系统,但相同的错误和可能相同的解决方案,即确保您的路径中的任何位置只有一个 geckodriver 实例
  • @Mark Lapierre 是的!我只有一个 geckodriver 实例。我在终端中输入了“which geckodriver”并将链接复制并粘贴到 executable_path="/path/to/geckodriver" 我不认为我有多个 firefox 驱动程序实例。我假设默认预装的 firefox 有自己的驱动程序,而 selenium 包有自己的一组浏览器驱动程序。
  • which -a geckodriver 只显示一个吗?即使在指定一个驱动程序后,其他问题之一仍然存在多个驱动程序的问题。
  • @MarkLapierre 是的。它只显示一个。
  • 嗯。我知道的唯一另一个问题是,如果您的操作系统不是 64 位,比如驱动程序。是吗?

标签: python selenium firefox ubuntu-14.04


【解决方案1】:

您应该提供已安装 geckodriver 的位置:

browser = webdriver.Firefox(executable_path="/path/to/geckodriver")

【讨论】:

  • 我仍然遇到同样的错误。我在终端中输入“which geckodriver”并复制并粘贴该链接以替换“/path/to/geckodriver”
【解决方案2】:

除了使用 Firefox,还有一种替代方法。我们可以改用 Chromium。以下是设置步骤:

sudo apt-get --yes --force-yes install chromium-browser xvfb unzip

wget -N http://chromedriver.storage.googleapis.com/2.26/chromedriver_linux32.zip
unzip chromedriver_linux32.zip
chmod +x chromedriver

sudo mv -f chromedriver /usr/local/share/chromedriver
sudo ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver
sudo ln -s /usr/local/share/chromedriver /usr/bin/chromedriver

【讨论】:

  • 这是如何解决问题的?他需要运行 Firefox,如果他对其他浏览器没问题,有很多可用的选项。
  • @user3251882 也许这是 Firefox 和 Selenium 的一个开放错误,所以如果有人在使用 linux 中预装的 Firefox 自动浏览网页时遇到问题,那么这是另一种选择。还是不能解决firefox的问题。可能是较低级别的驱动程序有问题...
【解决方案3】:

如果您使用的是 Mac OS,请确保您已在终端中运行此命令

chmod a+x geckodriver

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-12
    • 2020-06-26
    • 2017-10-23
    • 2016-04-03
    • 2021-06-10
    • 1970-01-01
    • 2017-05-26
    相关资源
    最近更新 更多