简体   繁体   English

如何在 ubuntu 16.04 上为 python3 selenium 安装 firefoxdriver webdriver?

[英]How to install firefoxdriver webdriver for python3 selenium on ubuntu 16.04?

I installed python3-selenium apt package on Ubuntu 16.04.我在 Ubuntu 16.04 上安装了 python3-selenium apt 包。 While installing, got a message:安装时,收到一条消息:

Suggested packages:
chromedriver firefoxdriver
The following NEW packages will be installed:
python3-selenium

When I try to run the following python code,当我尝试运行以下 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()

I get the following error:我收到以下错误:

Traceback (most recent call last): File "./test.py", line 19, in 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'回溯(最近一次调用):文件“./test.py”,第 19 行,在 main() 文件“./test.py”,第 11 行,在主浏览器中 = webdriver.Firefox(firefox_profile=getProfile())文件“/usr/lib/python3/dist-packages/selenium/webdriver/firefox /webdriver.py”,第 77 行,在init self.binary 中,超时),文件“/usr/lib/python3/dist-packages/selenium /webdriver/firefox/extension_connection.py”,第 47 行,在init self.profile.add_extension() 文件“/usr/lib/python3/dist-packages/selenium/webdriver/firefox/firefox_profile.py”,第 91 行,在add_extension self._install_extension(extension) 文件“/usr/lib/python3/dist-packages/selenium/webdriver/firefox/firefox_profile.py”,第 251 行,在 _install_extension 压缩文件 = zipfile.ZipFile(addon, 'r') 文件“ /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'

I did searching for packages name firefoxdriver in Ubuntu repositories but none exist.我确实在 Ubuntu 存储库中搜索了包名称 firefoxdriver 但不存在。 How do I solve this problem?我该如何解决这个问题?

Any help with installing the webdrivers appreciated!任何安装 webdrivers 的帮助表示赞赏!

The package you are missing is called firefox-geckodriver .您缺少的软件包称为firefox-geckodriver You can get it via sudo apt-get install firefox-geckodriver or by downloading from https://github.com/mozilla/geckodriver/releases , putting it into your $PATH (for example /usr/bin ) and making it executable.您可以通过sudo apt-get install firefox-geckodriver或从https://github.com/mozilla/geckodriver/releases下载,将其放入$PATH (例如/usr/bin )并使其可执行。

You can either upgrade to 16.10 (it's in yakkety) or you can download the deb from here (it works - I tried it).您可以升级到 16.10(在 yakkety 中),也可以从这里下载 deb(它有效 - 我试过了)。 Alternatively you can follow these instructions to install by hand (chromedriver but for Firefox it's the same).或者,您可以按照这些说明手动安装(chromedriver 但对于 Firefox,它是相同的)。

I'm not certain if this will fix the issue you're having, but you can give it a try.我不确定这是否会解决您遇到的问题,但您可以尝试一下。 But first, to answer the question as to where you can download firefoxdriver, my answer would be maybe firefoxdriver is not available, because the file is now called geckodriver, but it's really called Marionette Driver.但首先,要回答有关在哪里可以下载 firefoxdriver 的问题,我的答案可能是firefoxdriver 不可用,因为该文件现在称为 geckodriver,但它实际上称为 Marionette Driver。 It's described here: https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver , and it's made available here https://github.com/mozilla/geckodriver/releases它在这里描述: https : //developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver ,它在这里提供https://github.com/mozilla/geckodriver/releases

That answers the question, I believe, however I'd like to try to address the issue you're having.我相信这回答了问题,但是我想尝试解决您遇到的问题。

My understanding of your issue: You're trying to spawn an instance of firefox, through selenium, but it's not working.我对您的问题的理解:您试图通过 selenium 生成一个 firefox 实例,但它不起作用。

My hypothesis as to why it's not working: Maybe you're using Firefox 47 or newer.我关于它为什么不起作用的假设:也许您使用的是 Firefox 47 或更新版本。 If you're looking for where to downlad the new 'firefoxdriver' (its called the Marionette Driver, but the file is called geckodriver), you'll have to download the file directly from its github release page: https://github.com/mozilla/geckodriver/releases (I suggest downloading v0.9.0, since v0.10.0 hasn't worked for me).如果您正在寻找在哪里下载新的“firefoxdriver”(它称为 Marionette 驱动程序,但该文件称为 geckodriver),则必须直接从其 github 发布页面下载该文件: https://github。 com/mozilla/geckodriver/releases (我建议下载 v0.9.0,因为 v0.10.0 对我不起作用)。

If you're using Firefox 47 or newer, then starting up Firefox browser with a simple browser = webdriver.Firefox() just isn't going to work.如果您使用的是 Firefox 47 或更新版本,那么使用简单的browser = webdriver.Firefox()启动 Firefox 浏览browser = webdriver.Firefox()是行不通的。 This used to work for Firefox 46, and I'm assuming older versions, however it no longer works because support for Firefox Webdriver has now been dropped.这曾经适用于 Firefox 46,我假设是旧版本,但是它不再有效,因为现在已经放弃了对 Firefox Webdriver 的支持。 You now have to download the new Marionette driver, and modify your code a bit to make it work with this new driver.您现在必须下载新的 Marionette 驱动程序,并稍微修改您的代码以使其适用于这个新驱动程序。 You can learn more about Marionette in the link I provided above.您可以在我上面提供的链接中了解有关 Marionette 的更多信息。

The solutions I'm proposing You can either:我提出的解决方案您可以:

  1. Download and downgrade to Firefox 46下载并降级到 Firefox 46

or

  1. Download the new Marionette driver and adapt your code to work with it下载新的 Marionette 驱动程序并调整您的代码以使用它

If you choose option #1, then simply find a way to downgrade to Firefox 46.如果您选择选项 #1,那么只需找到一种降级到 Firefox 46 的方法。

If however you want your code to work with the most recent release of Firefox, then you choose option #2 and the basic gist of how to accomplish that is as follows:但是,如果您希望您的代码与最新版本的 Firefox 一起使用,那么您可以选择选项 #2 以及如何完成此操作的基本要点如下:

  1. Download and extract the driver下载并解压驱动程序
  2. Make sure that your OS can find the file in its systempath确保您的操作系统可以在其系统路径中找到该文件
  3. Modify your code to work with the new Marionette webdriver修改您的代码以使用新的 Marionette webdriver

The specific step by step process (for ubuntu) can be found in this stackoverflow answer launch selenium from python on ubuntu具体的分步过程(对于 ubuntu)可以在这个 stackoverflow answer launch selenium from python on ubuntu 中找到

selenium should be able to spawn firefox normally once that's done.完成后,selenium 应该能够正常生成 Firefox。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM