使用Selenium无法打开Firefox浏览器

4

当我尝试使用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

1
可能存在重复问题:https://dev59.com/vFkT5IYBdhLWcg3wRNet 和 https://dev59.com/jpXfa4cB1Zd3GeqPc0uF 不同的操作系统,但是相同的错误,也很可能有相同的解决方案,即确保您的路径中只有一个geckodriver实例。 - Mark Lapierre
1
@Mark Lapierre 是的!我只有一个geckodriver实例。我在终端中键入了“which geckodriver”并复制并粘贴了可执行路径链接到executable_path =“/path/to/geckodriver”。虽然我不认为我有多个firefox驱动程序实例。我假设默认预安装的firefox有自己的驱动程序,而selenium软件包有其自己的一组浏览器驱动程序。 - ykw
1
which -a geckodriver 只显示一个吗?其中一个问题即使指定了一个驱动程序,仍然存在多个驱动程序的问题。 - Mark Lapierre
1
@MarkLapierre 是的。它只显示一个。 - ykw
1
嗯。我知道的唯一另一个问题就是你的操作系统不是64位的,就像驱动程序一样。它是吗? - Mark Lapierre
1
@MarkLapierre 我的操作系统是64位。 - ykw
3个回答

0

使用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
@user3251882 或许这是Firefox和Selenium的一个已知问题,如果有其他人在Linux上使用预装的Firefox自动化浏览网页时也遇到了问题,那么这是另一种选择。但是仍然无法解决Firefox的问题。可能是底层驱动程序出现了问题... - ykw

0

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

chmod a+x geckodriver

0

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

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

我仍然收到相同的错误。我在终端中输入了“which geckodriver”,并复制并粘贴该链接以替换“/path/to/geckodriver”。 - ykw

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接