python - 使用 selenium webdriver 在 Windows 上设置 firefox 二进制文件的路径

标签 python firefox selenium selenium-webdriver

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

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 可执行文件的路径?

最佳答案

要设置 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()

关于python - 使用 selenium webdriver 在 Windows 上设置 firefox 二进制文件的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25713824/

相关文章:

python - python中的高速UART

python - 在 OpenCV Python 中读入一个向量作为 CV_8UC*

css - Firefox 和 IE11 中的 Fluid CSS 布局中断

python - 给定路径上没有 chrome 二进制文件 - MacOS - Selenium - Python

python - 在 ctypes.Structure 中使用枚举

python - 在 Python 中,为什么压缩元素在添加到列表时会分开?

firefox - 访问 Firefox 扩展中的全局变量

css - 整图鼠标悬停效果

.net - 将 Selenium 与 Powershell 结合使用 - 清除输入字段

java - 如何根据selenium webdriver中的其他元素选择一个元素