在Python中通过chromedriver设置Chrome浏览器二进制文件

22

我使用Python的Selenium Chrome webdriver。在我的代码中,我使用了:

driver = webdriver.Chrome(executable_path = PATH_TO_WEBDRIVER)

如何将webdriver指向webdriver可执行文件?是否有一种方法可以将webdriver指向Chrome浏览器二进制文件?

https://sites.google.com/a/chromium.org/chromedriver/capabilities中,他们有以下内容(我认为这就是我要找的内容):

ChromeOptions options = new ChromeOptions();
options.setBinary("/path/to/other/chrome/binary");

有没有Python的例子?

4个回答

42

您可以通过以下不同的方式,在使用 Python 的 ChromeDriver 时设置 Chrome 浏览器二进制文件位置:


使用选项

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.binary_location = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"
driver = webdriver.Chrome(chrome_options=options, executable_path="C:/Utility/BrowserDrivers/chromedriver.exe", )
driver.get('http://google.com/')

使用DesiredCapabilities

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
cap = DesiredCapabilities.CHROME
cap = {'binary_location': "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"}
driver = webdriver.Chrome(desired_capabilities=cap, executable_path="C:\\Utility\\BrowserDrivers\\chromedriver.exe")
driver.get('http://google.com/')

使用 Chrome 作为服务

from selenium import webdriver
import selenium.webdriver.chrome.service as service
service = service.Service('C:\\Utility\\BrowserDrivers\\chromedriver.exe')
service.start()
capabilities = {'chrome.binary': "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"}
driver = webdriver.Remote(service.service_url, capabilities)
driver.get('http://www.google.com')

看起来朝着正确的方向。我正在使用Mac电脑。我尝试下载Chrome浏览器二进制文件,但我找到的唯一东西是Chromium二进制文件,如果我尝试使用它,驱动程序会出现错误“未找到Chrome二进制文件”。另一方面,我尝试将Chrome从/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome移动到本地使用,但是驱动程序就会卡住。 - user123
@user123 请从正确的位置获取ChromeDriver https://sites.google.com/a/chromium.org/chromedriver 谢谢 - undetected Selenium
1
我在ChromeOptions的文档中看到了binary_location,因此我接受了答案。现在,我仍然苦于找到适用于Mac的正确chrome可执行文件。 - user123
我只看到了Chromedriver的下载,而没有实际的Chrome浏览器。 - user123
@user123 如果我没错的话,你想要使用多个版本的 Chrome 浏览器,对吧?那么我想你只能从 Chromium 网站下载。我也是用 Mozilla Firefox 做同样的事情 :) - undetected Selenium
显示剩余2条评论

1

非常感谢,我曾经因为不知道如何在Python中设置Chrome可执行路径而苦恼了2.5小时。现在可以运行了。

options = Options()
options.binary_location = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"
driver = webdriver.Chrome(chrome_options=options, executable_path="C:/Utility/BrowserDrivers/chromedriver.exe", )

1

有没有办法将webdriver指向Chrome浏览器二进制文件?

正如其他人已经提到的那样,使用 binary_location。但是,Chrome的位置会根据平台而变化。Fedora和Ubuntu使用不同的位置。因此,您可能想使用类似以下的内容:

def get_chrome():
    if os.path.isfile('/usr/bin/chromium-browser'):
        return '/usr/bin/chromium-browser'
    elif os.path.isfile('/usr/bin/chromium'):
        return '/usr/bin/chromium'
    elif os.path.isfile('/usr/bin/chrome'):
        return '/usr/bin/chrome'
    elif os.path.isfile('/usr/bin/google-chrome'):
        return '/usr/bin/google-chrome'
    else:
        return None

然后:

if version.parse(selenium.__version__) >= version.parse("3.0"):
    opts = Options()
    opts.binary_location = get_chrome()
    opts.add_argument('--headless')
    opts.add_argument('--no-sandbox')
    opts.add_argument('--disable-dev-shm-usage')

    driver = webdriver.Chrome(chrome_options=opts)
    driver.maximize_window()
else:
    opts = Options()
    opts.headless = True
    opts.binary_location = get_chrome()

    driver = webdriver.Chrome(chrome_options=opts)
    driver.maximize_window()

agent = driver.execute_script('return navigator.userAgent')

-1

首先,如果您想使用Chrome,则需要从以下网址下载其二进制文件:

https://sites.google.com/a/chromium.org/chromedriver/

现在你需要将这个驱动路径传递给Selenium WebDriver。

如果你使用Python,代码应该像下面这样:

    driver = webdriver.Chrome('C:\Users\name\Downloads\chromedriver_win32 (3)\chromedriver.exe')
    driver.implicitly_wait(30) # seconds
    driver.get('https://www.google.co.in/')

希望能对你有所帮助 :)

实际上,这不是我要找的。Chromedriver二进制文件和Chrome浏览器二进制文件是两个不同的东西。对于Chromedriver,我已经有一个可用的代码。我指的是Chrome浏览器二进制文件。 - user123

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接