selenium使用Chrome浏览器

准备:电脑上要安装Chrome浏览器,并且安装对应版本的Chromedriver。
https://npm.taobao.org/mirrors/chromedriver

部分Chromedriver支持的Chrome版本对照表:

chromedriver版本支持的Chrome版本
v2.41v67-69
v2.40v66-68
v2.39v66-68
v2.38v65-67
v2.37v64-66
v2.36v63-65
v2.35v62-64
v2.34v61-63
v2.33v60-62
v2.32v59-61
v2.31v58-60
v2.30v58-60
v2.29v56-58
v2.28v55-57
v2.27v54-56
v2.26v53-55
v2.25v53-55
v2.24v52-54
v2.23v51-53
v2.22v49-52
v2.21v46-50

使用有界面的Chrome浏览器:

In [1]: from selenium import webdriver

In [2]: driver = webdriver.Chrome()

In [3]: driver.get('https://www.baidu.com')

In [5]: driver.find_element_by_id('kw').send_keys('selenium')

In [6]: driver.find_element_by_id('su').click()

使用无界面的Chrome浏览器:

In [9]: from selenium import webdriver

In [11]: option = webdriver.ChromeOptions()

In [12]: option = webdriver.ChromeOptions()
    	option.add_argument('--no-sandbox')  
    	option.add_argument('--headless') 

In [13]: driver = webdriver.Chrome(options=option)

In [14]: driver.get('https://www.baidu.com')

In [15]: driver.find_element_by_id('kw').send_keys('selenium')

In [16]: driver.find_element_by_id('su').click()

In [17]: driver.save_screenshot('baidu.png')
Out[17]: True

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值