基于Splinter演示如何使用Chrome WebDriver

发布于:2024-04-29 ⋅ 阅读:(25) ⋅ 点赞:(0)

关注开源优测不迷路

大数据测试过程、策略及挑战

测试框架原理,构建成功的基石

在自动化测试工作之前,你应该知道的10条建议

在自动化测试中,重要的不是工具

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

pip install selenium
pip install splinter

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

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

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在下建议用下面的命令进行安装

brew install chromedriver

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

$ 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下建议以下命令进行安装

$ 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下通用的安装命令如下:

‍‍
$ 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:

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 的模式,从而开启实验性的仿真模式。

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等最新版本的零基础系列,点个赞支持我哦~~~


网站公告

今日签到

点亮在社区的每一天
去签到