【问题标题】:Setting path to firefox binary on windows with selenium webdriver使用 selenium webdriver 在 Windows 上设置 firefox 二进制文件的路径
【发布时间】:2014-11-01 01:21:50
【问题描述】:

我正在尝试构建一个实用函数来将漂亮的汤代码输出到浏览器我有以下代码:

def bs4_to_browser(data):

    from selenium import webdriver

    driver = webdriver.Firefox(path="F:\FirefoxPortable\Firefox.exe")
    driver.get("about:blank")

    data = '<h1>test</h1>'  # supposed to come from BeautifulSoup
    driver.execute_script('document.body.innerHTML = "{html}";'.format(html=data))

    return

当我运行它时,我得到:

TypeError at /providers/
__init__() got an unexpected keyword argument 'path'

我用的是win7。如何设置可移植 firefox 可执行文件的路径?

【问题讨论】:

    标签: python firefox selenium selenium-webdriver


    【解决方案1】:

    要将自定义路径设置为Firefox,您需要使用FirefoxBinary

    from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
    
    binary = FirefoxBinary('F:\FirefoxPortable\Firefox.exe')
    driver = webdriver.Firefox(firefox_binary=binary)
    

    或者,或者,将F:\FirefoxPortable 添加到PATH 环境变量并以通常的方式启动Firefox

    driver = webdriver.Firefox()
    

    【讨论】:

    • 谢谢,距离更近了,但是我收到一个弹出窗口:“无法加载您的 Firefox 配置文件。它可能丢失或无法访问。”同样在 python 中,我收到一个 webdriver 错误:“消息:'浏览器似乎在我们可以连接之前已经退出。输出是:'”
    • @user61629 好,你要发布什么版本的 Firefox?如果它是全新的(第 32 位)- 将其降级到至少 31(为了安全起见,最好是 28)。另外,请确保您安装了最新的 selenium 软件包。
    • @user61629 Firefox 32nd 对于最新的 selenium 来说太新了。尝试 31 日或 28 日。让我知道它是否有帮助。
    • 删除版本 32 后,我能够找到 firefox 便携式版本 31。这确实有效。非常感谢!
    【解决方案2】:

    默认情况下,selenium 会查看路径 - C:\Program Files (x86)\Mozilla Firefox\

    请使用链接安装 Firefox - http://filehippo.com/download_firefox/67599/ 并尝试

    为此,您无需提供二进制文件。

    如果您想在自定义位置安装 Firefox,请在弹出位置时根据您的意愿指定目录。如果您安装在自定义位置,那么我们需要在代码中提及 Firefox 二进制位置,如下所示

    from selenium import webdriver
    from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
    
    binary = FirefoxBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe")
    fp = webdriver.FirefoxProfile()
    driver = webdriver.Firefox(firefox_binary=binary, firefox_profile=fp)
    

    【讨论】:

      【解决方案3】:

      例如,如果您已经下载了 chrome 驱动程序,您可以像这样指定它的路径:

      from selenium import webdriver
      driver = webdriver.Chrome(r'D:\\chromedriver.exe')
      

      【讨论】:

        猜你喜欢
        • 2017-06-24
        • 2020-10-24
        • 2017-09-22
        • 1970-01-01
        • 2018-02-21
        • 1970-01-01
        • 2022-01-13
        • 2016-06-25
        • 1970-01-01
        相关资源
        最近更新 更多