当前位置: 首页 > 面试题库 >

通过python中的chromedriver设置Chrome浏览器二进制文件

万俟承望
2023-03-14
问题内容

我将Selenium与Python 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的范例吗?


问题答案:

您可以使用以下几种方法通过使用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')


 类似资料:
  • 我将Selenium与Python Chrome webdriver一起使用。在我的代码中,我使用了: 将webdriver指向webdriver可执行文件。有没有办法将webdriver指向Chrome浏览器二进制文件? https://sites.google.com/a/chromium.org/chromedriver/capabilities他们有以下产品(我想这就是我要找的): 有人举

  • 我试图在python中使用selenium chromedriver创建网站www.mouser.co.uk。但从第一枪就被检测为bot。 有人对此有解释吗?。以下是我使用的代码:

  • 我突然遇到了硒和铬驱动的错误问题。我还没有改变一件事,但我遇到了这些错误信息。这个脚本在几个小时前就开始工作了,现在没有任何调整,它不工作了。 这是我对应的代码: 更奇怪的是,当打开新终端、加载python并从selenium import webdriver中键入时,我没有收到任何错误。但是,当我导航到脚本所在的文件夹,并从selenium import webdriver加载python和类型

  • 问题内容: 我想知道,在IE和Firefox中,您是否可以设置浏览器,以允许跨域调用。 我在chrome中找不到任何选项(实际上,通常根本没有太多选项…) 有关于about:config之类的东西吗? 亲切的问候 -安迪 问题答案: 不好意思 Chrome和Firefox通过W3C CORS规范(跨源资源共享)支持跨域请求,但是远程主机必须启用它。如果远程主机明确支持它,那么您不必对XMLHttp

  • 问题内容: 我正在研究用于网络抓取的python脚本,并且没有使用Chromedriver作为软件包之一。我希望此操作无需任何弹出窗口即可在后台运行。我在chromedriver上使用了“无头”选项,它似乎在不显示浏览器窗口的情况下完成了工作,但是,我仍然看到.exe文件正在运行。查看我在说什么的屏幕截图。屏幕截图 这是我用来启动ChromeDriver的代码: 我尝试做的事情是将选项中的窗口大小