问题
在使用python selenium控制chrome浏览器操作的过程中,由于安装的chrome浏览器的版本找不到对应版本的驱动chromedriver.exe文件,下载了小几个版本号的驱动软件。发现运行下面的代码是无法正常使用的:
from selenium import webdriver driver = webdriver.chrome()
报错内容如下:
there was an error managing chromedriver (request or response body error: operation timed out);
webdriverexception traceback (most recent call last) cell in[4], line 1----> 1 driver =webdriver.chrome()。
webdriverexception: message: unknown error: cannot find chrome binary
主要就是运行
driver = webdriver.chrome()
的时候报错,我一直以为是chrome版本和chromedriver版本不一致的问题,所以特意在cnpm binaries mirror下载了对应版本的chrome浏览器。但是其实也不行。可能的原因是浏览器没有进行默认安装的方式。因为我这的浏览器都是解压即用的,所以要解决就需要重新下载安装浏览器了。这里我不太想采用这种方式,所以找了其它方法。
解决方法
采用的方式是指定chrome浏览器程序路径的方式:
from selenium import webdriver chrome_opt= webdriver.chromeoptions() chrome_opt.binary_location = "e:\****\google\chrome\application\chrome.exe" driver = webdriver.chrome(chrome_opt) url = "https://www.baidu.com/" driver.get(url)
这里通过webdriver.chromeoptions() 新建了chrome浏览器的选项,然后通过binary_location设置chrome浏览器程序的路径。
采用这种方式就指定了浏览器的路径,能够顺利控制浏览器了,我这里测试过两个版本的浏览器都是可以控制的:
其它常用选项参数:
add_argument(argument):添加命令行参数。 >window_size:设置浏览器窗口的大小。
disable_extensions:禁用扩展程序。 >binary_location:设置chrome浏览器可执行文件的路径。
其它详细说明请看:https://www.selenium.dev/zh-cn/documentation/webdriver/drivers/options/
到此这篇关于python selenium无法打开chrome浏览器处理自定义浏览器路径的文章就介绍到这了,更多相关python selenium无法打开chrome浏览器内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论