前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于Splinter演示如何使用Chrome WebDriver

基于Splinter演示如何使用Chrome WebDriver

作者头像
苦叶子
发布2024-04-26 20:26:10
910
发布2024-04-26 20:26:10
举报
文章被收录于专栏:开源优测开源优测

Chrome WebDriver由selenium提供的chrome浏览器驱动,在使用它前,需要先安装selenium,可以通过pip命令进行安装

代码语言:javascript
复制
pip install selenium
pip install splinter

首先请先确保你的电脑已经安装了chrome浏览器。

我们可以在自定义路径中使用chrome,不过你需要将可执行路径作为字典传递给**kwargs参数,将executable_path作为字典的key值,将可执行文件的路径设置为字典的Value

代码语言:javascript
复制
from splinter import Browser

# /path/to/chrome为chrome浏览器的实际路径
executable_path = {'executable_path': '/path/to/chrome'}


browser = Browser('chrome', **executable_path)

设置Chrome WebDriver

在Splinter中使用chrome,我们需要先安装selenium,同时确保安装Chrome Webdriver.

在Mac在下建议用下面的命令进行安装

代码语言:javascript
复制
brew install chromedriver

在linux32下建议用以下命令进行安装

代码语言:javascript
复制
$ cd $HOME/Downloads 
$ wget https://chromedriver.googlecode.com/files/chromedriver_linux32_20.0.1133.0.zip 
$ unzip chromedriver_linux32_20.0.1133.0.zip

在linux64下建议以下命令进行安装

代码语言:javascript
复制
$ cd $HOME/Downloads 
$ wget https://chromedriver.googlecode.com/files/chromedriver_linux64_20.0.1133.0.zip 
$ unzip chromedriver_linux64_20.0.1133.0.zip

注:20.0.1133.0这个是版本号,可以根据你的实际需要,安装合适的版本

linux下通用的安装命令如下:

代码语言:javascript
复制
$ mkdir -p $HOME/bin 
$ mv chromedriver $HOME/bin 
$ echo "export PATH=$PATH:$HOME/bin" >>$HOME/.bash_profile

通过这个命令会安装最新版本的驱动。

windows用户,则需通过一下链接去手动下载对应的版本

https://code.google.com/p/chromedriver/downloads/list

使用Chrome WebDriver

要使用 Chrome driver, 你只需要在创建 Browser 实例时传入字符串 chrome:

代码语言:javascript
复制
from splinter import Browser 
browser = Browser('chrome')

注意: 如果你不为 Browser 指定 driver, 那么会默认使用 firefox。

注意: 如果你在使用 $HOME/.bash_profile 时遇到问题, 可以试试 $HOME/.bashrc。

使用 Chrome headless

从Chrome 59开始,我们可以运行 Chrome 作为一个 headless 浏览器。

from splinter import Browser browser = Browser('chrome', headless=True)

使用 Chrome 仿真模式

可以通过 Chrome options 定制 Chrome 的模式,从而开启实验性的仿真模式。

代码语言:javascript
复制
from selenium import webdriver 
from splinter import Browser 
mobile_emulation = {"deviceName": "Google Nexus 5"} 
chrome_options = webdriver.ChromeOptions() 
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
browser = Browser('chrome', options=chrome_options)

详细内容请参考 chrome driver documentation:https://sites.google.com/a/chromium.org/chromedriver/mobile-emulation

接下来将为大家,重写selenium、pytest、playwright、robotframework、jmeter等最新版本的零基础系列,点个赞支持我哦~~~

本文参与?腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2024-04-22,如有侵权请联系?cloudcommunity@tencent.com 删除

本文分享自 开源优测 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com