Python 使用 selenium webdriver 在 windows 上设置 firefox 二进制文件的路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25713824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting path to firefox binary on windows with selenium webdriver
提问by user61629
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. How to I set the path to the portable firefox executable?
我用的是win7。如何设置便携式 Firefox 可执行文件的路径?
采纳答案by alecxe
To set the custom path to Firefoxyou 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:\FirefoxPortableto the PATHenvironment variable and fire up Firefoxin a usual way:
或者,或者,添加F:\FirefoxPortable到PATH环境变量并Firefox以通常的方式启动:
driver = webdriver.Firefox()
回答by Karthikeya
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. If you installed in custom location then we need to mention Firefox binary location in the code as below
如果您想在自定义位置安装 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)

