问题
在使用python selenium控制chrome浏览器操作的过程中,由于安装的chrome浏览器的版本找不到对应版本的驱动chromedriver.exe文件,下载了小几个版本号的驱动软件。发现运行下面的代码是无法正常使用的:
from selenium import webdriver
driver = webdriver.chrome()
报错内容如下:
主要就是运行
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浏览器程序的路径。
采用这种方式就指定了浏览器的路径,能够顺利控制浏览器了,我这里测试过两个版本的浏览器都是可以控制的:
其它常用选项参数:
其它详细说明请看:https://www.selenium.dev/zh-cn/documentation/webdriver/drivers/options/
发表评论