如何通过 Python selenium 在多个 firefox 二进制文件中选择要使用的 firefox 可执行文件
How can I choose which firefox executable to use among multiple firefox binaries through Python selenium
我在 Mac OS X (v 10.11.6) 和 firefox (v 59.0.3) 上使用 python (v 3.65) selenium (v3.11.0)和壁虎驱动程序(v 0.20.1)。我在应用程序文件夹中有我常用的 firefox,在另一个文件夹中有第二个 firefox。我怎样才能告诉 python selenium 使用第二个 firefox 而不是去应用程序中的那个?
如果可能的话,除了 firefox/geckodriver 之外,我更喜欢可以推广到其他浏览器的答案。
要在多个 Firefox 可执行文件中选择并使用其中一个,您可以使用来自 [=21] 的参数 binary_location =]firefox.options。作为以下代码块中的示例,我使用 Firefox Nightly 二进制文件打开 Firefox Nightly 浏览器:
代码块:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.binary_location = r'C:\Program Files\Firefox Nightly\firefox.exe'
driver = webdriver.Firefox(firefox_options=options, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get('http://google.com/')
print("Firefox Browser Invoked")
driver.quit()
控制台输出:
Firefox Browser Invoked
我在 Mac OS X (v 10.11.6) 和 firefox (v 59.0.3) 上使用 python (v 3.65) selenium (v3.11.0)和壁虎驱动程序(v 0.20.1)。我在应用程序文件夹中有我常用的 firefox,在另一个文件夹中有第二个 firefox。我怎样才能告诉 python selenium 使用第二个 firefox 而不是去应用程序中的那个?
如果可能的话,除了 firefox/geckodriver 之外,我更喜欢可以推广到其他浏览器的答案。
要在多个 Firefox 可执行文件中选择并使用其中一个,您可以使用来自 [=21] 的参数 binary_location =]firefox.options。作为以下代码块中的示例,我使用 Firefox Nightly 二进制文件打开 Firefox Nightly 浏览器:
代码块:
from selenium import webdriver from selenium.webdriver.firefox.options import Options options = Options() options.binary_location = r'C:\Program Files\Firefox Nightly\firefox.exe' driver = webdriver.Firefox(firefox_options=options, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe') driver.get('http://google.com/') print("Firefox Browser Invoked") driver.quit()
控制台输出:
Firefox Browser Invoked