简体   繁体   English

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

[英]Setting path to firefox binary on windows with selenium webdriver

I am trying to build a utility function to output beautiful soup code to a browser I have the following code:我正在尝试构建一个实用函数来将漂亮的汤代码输出到浏览器我有以下代码:

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

when I run this I get:当我运行这个时,我得到:

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

I am using win7.我用的是win7。 How to I set the path to the portable firefox executable?如何设置便携式 Firefox 可执行文件的路径?

To set the custom path to Firefox you need to use FirefoxBinary :要设置Firefox的自定义路径,您需要使用FirefoxBinary

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

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

Or, alternatively, add F:\\FirefoxPortable to the PATH environment variable and fire up Firefox in a usual way:或者,将F:\\FirefoxPortable添加到PATH环境变量并以通常的方式启动Firefox

driver = webdriver.Firefox()

By default selenium will look into the path - C:\\Program Files (x86)\\Mozilla Firefox\\默认情况下,selenium 会查看路径 - C:\\Program Files (x86)\\Mozilla Firefox\\

Please install Firefox using the link - http://filehippo.com/download_firefox/67599/ and try请使用链接安装 Firefox - http://filehippo.com/download_firefox/67599/并尝试

For this, you no need to give the binary.为此,您无需提供二进制文件。

If you want to install Firefox in custom location then give the directory as your wish when it pops up for location.如果您想在自定义位置安装 Firefox,请在弹出位置时根据您的意愿提供该目录。 If you installed in custom location then we need to mention Firefox binary location in the code as below如果您安装在自定义位置,那么我们需要在代码中提及 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)

If you for example downloaded the chrome driver already, you can just specify the path to it like that:例如,如果您已经下载了 chrome 驱动程序,则可以像这样指定它的路径:

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

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

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