我在 Ubuntu 16.04 上安装了 python3-selenium apt 包。安装时,收到一条消息:
Suggested packages:
chromedriver firefoxdriver
The following NEW packages will be installed:
python3-selenium
当我尝试运行以下 Python 代码时,
#! /usr/bin/python3.5
from selenium import webdriver
import time
def get_profile():
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.privatebrowsing.autostart", True)
return profile
def main():
browser = webdriver.Firefox(firefox_profile=getProfile())
#browser shall call the URL
browser.get("http://www.google.com")
time.sleep(5)
browser.quit()
if __name__ == "__main__":
main()
我收到以下错误:
Traceback (most recent call last):
File "./test.py", line 19, in <module>
main()
File "./test.py", line 11, in main
browser = webdriver.Firefox(firefox_profile=getProfile())
File "/usr/lib/python3/dist-packages/selenium/webdriver/firefox /webdriver.py", line 77, in __init__
self.binary, timeout),
File "/usr/lib/python3/dist-packages/selenium/webdriver/firefox/extension_connection.py", line 47, in __init__
self.profile.add_extension()
File "/usr/lib/python3/dist-packages/selenium/webdriver/firefox/firefox_profile.py", line 91, in add_extension
self._install_extension(extension)
File "/usr/lib/python3/dist-packages/selenium/webdriver/firefox/firefox_profile.py", line 251, in _install_extension
compressed_file = zipfile.ZipFile(addon, 'r')
File "/usr/lib/python3.5/zipfile.py", line 1009, in __init__
self.fp = io.open(file, filemode)
FileNotFoundError: [Errno 2] No such file or directory: '/usr/lib /firefoxdriver/webdriver.xpi'
我确实在 Ubuntu 存储库中搜索了名为 firefoxdriver 的软件包,但不存在。
非常感谢您对安装 webdrivers 的任何帮助。
答案1
您需要下载最新的 geckodriver,然后解压文件并将其放在您的路径下(即~/.local/bin
)。
这将解决异常,但您的程序将在以下行之后挂起:
browser = webdriver.Firefox(firefox_profile=getProfile())
我发现的唯一解决办法是降级 Firefox。
首先,列出可用的版本:
apt-cache policy firefox
然后安装低于 49 的版本(45.0.2+build1-0ubuntu1
对我来说):
sudo apt install firefox=45.0.2+build1-0ubuntu1
你可以改用铬...