1、selenium的介绍
官方网站:https://www.selenium.dev/
2、selenium的架构图
3、selenium的环境配置步骤
4、driver的配置
chromedriver与chrome的对应关系表:https://huilansame.github.io/huilansame.github.io/archivers/chromedriver-to-chrome-version
chrome版本与对应的谷歌驱动(chromedriver):
https://www.cnblogs.com/yfacesclub/p/8482681.html
chromedriver:http://chromedriver.storage.googleapis.com/index.html
淘宝镜像:http://npm.taobao.org/mirrors/chromedriver
geckodriver:https://github.com/mozilla/geckodriver/releases
建议将驱动直接放在python的根目录下,如果用的venv环境,也要放在venv环境下
5、如何在python调用selenium
6、seleniumIDE的下载和安装
7、IDE的启动
8、录制第一个用例
9、IDE的使用
10、管理用例
11、扩展(代码的导出功能)
12、python selenium文档(用例编写)
13、用例的关键要素
14、selenium 的三种等待方式
直接等待:time.sleep(3) ——不建议使用,遇到网络不好的情况会超时
隐式等待:self.driver.implicitly_wait(3) ——缺点:这是全局等待,会造成不同场景不好设置等待时间,多长或者过端都不好
显式等待:WebDriverWait配合until()和until_not()方法,根据判断条件进行等待
实战演练
“”"
import pytest
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions
class TestPut():
def setup(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.driver.implicitly_wait(5)
def teardown(self):
sleep(5)
self.driver.quit()
def test_case_a(self):
self.driver.get("https://blog.csdn.net/weixin_44275820?t=1")
WebDriverWait(self.driver, 10).until(
expected_conditions.element_to_be_clickable(By.XPATH, '//*[@id="mainBox"]/main/div[2]/div[2]/h4/a'))
self.driver.find_element(By.XPATH, '//*[@id="mainBox"]/main/div[2]/div[1]/h4/a').click()
if name == ‘main’:
pytest.main([’-v’, ‘-s’])
“”"
15、web控件定位与常见操作
selenium的输入与点击
输入内容:find_element(By.ID,‘123’).send_keys(“输入你想要的内容”)
点击:find_element(By.ID,‘456’).clcik()
xpath定位
优点:无论是selenium还是appium都可以定位,是一个万能的定位方式
缺点:运行速度比css慢很多,因为它是从头到尾的去遍历
在浏览器的控制台上可以用$x()查看xpath定位是否成功
x(′//∗[@id="blogTitle"]//a[1]′)![在这里插入图片描述](https://img−blog.csdnimg.cn/20200330211322524.png?x−oss−process=image/watermark,typeZmFuZ3poZW5naGVpdGk,shadow10,textaHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDI3NTgyMA==,size16,colorFF0000,t70)css定位(适用于css样式定位)css定位不使用与appium,因为appium原生的框架不支持,但是当app嵌入了网页时,这个时候可以用css定位![在这里插入图片描述](https://img−blog.csdnimg.cn/20200330211929493.png?x−oss−process=image/watermark,typeZmFuZ3poZW5naGVpdGk,shadow10,textaHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDI3NTgyMA==,size16,colorFF0000,t70)![在这里插入图片描述](https://img−blog.csdnimg.cn/20200330212035724.png?x−oss−process=image/watermark,typeZmFuZ3poZW5naGVpdGk,shadow10,textaHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDI3NTgyMA==,size16,colorFF0000,t70)在浏览器的控制台上可以用()查看xpath定位是否成功
$(’[id=blogTitle]’)
其余定位方式暂不介绍,无论时ID定位还是NAME定位,都是基于css定位封装的
16、web控件交互
官方文档:https://selenium-python.readthedocs/io.api.html