当前位置:   article > 正文

Python Selenium无法打开Chrome浏览器处理自定义浏览器路径_there was an error managing chromedriver (error de

there was an error managing chromedriver (error decoding response body); usi

问题

在使用Python Selenium控制Chrome浏览器操作的过程中,由于安装的Chrome浏览器的版本找不到对应版本的驱动chromedriver.exe文件,下载了小几个版本号的驱动软件。发现运行下面的代码是无法正常使用的:

from selenium import webdriver
driver = webdriver.Chrome()
  • 1
  • 2

报错内容如下:
在这里插入图片描述

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()
  • 1

的时候报错,我一直以为是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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

这里通过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/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/888194
推荐阅读
相关标签
  

闽ICP备14008679号