selenium简介
官网
简介
用于web浏览器测试的工具
支持的浏览器包括IE,Firefox,Chrome,edge等
使用简单,可使用java,python等多种语言编写用例脚本
主要由三个工具构成,webdriver,IDE,
web自动化环境配置步骤
1、具有python环境
2、安装selenium依赖
3、driver的下载与配置
selenium依赖安装
前提:配置好python环境,配置好pip工具
安装:
pip install selenium

如果遇见如下报错,可以选择使用国内镜像源
pip install selenium -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
或者

driver的下载与配置步骤
1、下载浏览器
(本教程以 chrome 为主,下载地址:
https://www.google.cn/chrome/
),双击安装即可
2、查看浏览器版本


3、下载对应版本driver
下载地址:
http://chromedriver.storage.googleapis.com/index.html


根据自己的系统选择chromedriver,windows 只有32位的,因为Windows 64为也支持运行32为的。点击下载保存并进行解压,记下chromedriver 的路径

4、配置driver的环境变量

5、重启命令行工具,验证是否配置成功

在cmd里面输入
chromedriver --version

在pycharm中验证
from selenium import webdriver
# 注意Chrome后面要添加括号,代表实例化这个类
driver = webdriver.Chrome()
# 打开网址
driver.get("https://www.baidu.com/")
# 关闭driver
driver.quit()
遇到的问题
1、执行代码后,浏览器在未加入driver.quit()时,出现闪退现象,
通过如下设置也未生效
# 设置执行完程序后不自动关闭浏览器
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
经排查原因发现selenium为4.8.2版本过高导致,安装4.1.1版本后正常


2、浏览器打开后没有打开到最大
设置如下配置未生效
# 浏览器窗口最大化
options.add_argument('--start-maximized')
优化代码后生效
# 最大化当前页
driver.maximize_window()
time.sleep(2)
# 刷新当前页面
driver.refresh()
time.sleep(2)
最终验证代码如下
from selenium import webdriver
import time
# 注意Chrome后面要添加括号,代表实例化这个类
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
# 最大化当前页
driver.maximize_window()
time.sleep(2)
# 刷新当前页面
driver.refresh()
time.sleep(2)
# 关闭driver
driver.quit()
