Selenium配置chrome浏览器

依旧是记录自己配置的东西,防止之后忘了找不到

如果使用:

from selenium import webdriver

#下面浏览器选其一即可
# Firefox浏览器
driver = webdriver.Firefox()
# Chrome浏览器
driver = webdriver.Chrome()
# Internet Explorer浏览器
driver = webdriver.Ie()
# Edge浏览器
driver = webdriver.Edge()
# Opera浏览器
driver = webdriver.Opera()
# PhantomJS
driver = webdriver.PhantomJS()

# 打开网页,get中放url
driver.get("http://www.baidu.com")

可以直接打开chrome浏览器,那就可以直接用,但是一般来说都是缺配置的,会报错:

selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary

selenium启动配置参数接收是ChromeOptions类,需要webdriver.exe的驱动
所以最后代码如下:

options = webdriver.ChromeOptions()
#location放自己chrome.exe的地址
options.binary_location = r"C:\Program Files\Google\Chrome\Application\chrome.exe"

#service中放自己chromedriver.exe的地址
#driver = webdriver.Chrome(r"D:\chrome\chromedriver\chromedriver.exe")
services = Service(r"D:\chrome\chromedriver\chromedriver.exe")
driver = webdriver.Chrome(service=services)
driver.get("http://www.baidu.com")

chrome需要对应版本的chromedriver,这里提供一个参考:
Selenium 与(Firefox、GeckoDriver)和(Chrome、ChromeDriver)版本对应关系
以及:chromedriver下载地址
查看自己的chrome版本:在chrome浏览器地址栏输入下列命令得到相应的信息

about:version

在这里插入图片描述
到这里再运行代码就没有问题了,其他配置可以参考
selenium启动Chrome配置参数问题
但是我是没配置其他东西就可以正常运行的

题外话:

driver = webdriver.Chrome(r"D:\chrome\chromedriver\chromedriver.exe")

之所以被注释掉,是因为在运行的时候报问题(但是不影响运行):

DeprecationWarning: executable_path has been deprecated, please pass in a Service object

出现 DeprecationWarning的警告大多属于版本更新时,所使用的方法过时的原因,他在当前版本被重构,还可以传入参数,但是在之后的某个版本会被删除,所以要换一种方法,即:

services = Service(r"D:\chrome\chromedriver\chromedriver.exe")
driver = webdriver.Chrome(service=services)

是参考:爬虫 - 解决 Executable path has been deprecated please pass in a Service object in Selenium Python 问题

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值