1.1 让开发屏蔽掉验证码
1.2 使用万能验证码
1.3 从数据库读取验证码
1.4 验证码识别
2Select 下拉框Selenium 通过 Select 类来操作由 Select 标签实现的下拉框,引入类:from selenium.webdriver.support.select import Select
获取所有选项:
·select.options
返回所有被选中的选项:
·select.all_selected_options
选择下拉列表值:
·通过下标选择值:select_by_index(index),下标从0开始
·通过 value 属性:select_by_value(value)
·通过文本:select_by_visible_text(文本内容)
取消选中:
·根据value取消选中:deselect_by_value(value)
·根据index取消选中:deselect_by_index(0)
·根据标签文本选中:deselect_by_visible_text(文本内容)
·取消所有选中项:deselect_all()
示例代码:
<html><head> <meta charset="UTF-8"> <title>Titletitle>head><body> <select name="test" id="test"> <option value="java">javaoption> <option value="python">pythonoption> <option value="PHP">PHPoption> <option value="code">测试option> select> <select id="city" multiple> <option value="guangzhou">广州option> <option value="changsha">长沙option> <option value="beijing">北京option> select>body>html>
from time import sleepfrom selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.select import Selectfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECdriver = webdriver.Chrome()driver.get('http://localhost:63342/ApiTesting/tests/demo.html')sleep(1)# 1. 定位到 select 元素locator = (By.ID, "test")WebDriverWait(driver, 10).until(EC.visibility_of_element_located(locator))ele = driver.find_element(*locator)# 2. 实例化select类s = Select(ele)# 返回所有选项for option in s.options: print(option.text)# 返回所有被选中的选项for option in s.all_selected_options: print(option.text)# 3. 选择下拉列表值# 方式一:根据下标选取值,下标从0开始s.select_by_index(2)# 方式二:value值s.select_by_value("python")# 方式三:文本s.select_by_visible_text('测试')# 取消操作只适用于添加了multiple的下拉框city = Select(driver.find_element_by_id("city"))# 全选for option in city.options: if not option.is_selected(): city.select_by_visible_text(option.text)sleep(1)# 方式一:根据value取消选中city.deselect_by_value("beijing")# 方式二:根据index取消选中city.deselect_by_index(0)# 方式三:根据标签文本选中city.deselect_by_visible_text("长沙")sleep(1)# 全选for option in city.options: if not option.is_selected(): city.select_by_visible_text(option.text)sleep(1)# 取消选中所有选项city.deselect_all()driver.quit()
3JS 代码3.1 JS 实现页面滚动
示例代码:
import timefrom selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECdriver = webdriver.Chrome()driver.get('https://www.baidu.com/')driver.maximize_window()driver.find_element_by_id('kw').send_keys('豆瓣', Keys.ENTER)driver.find_element_by_id('su').click()# 1. 找到要滚动到可视区域的元素locator = (By.XPATH, "//a[text()='- 知乎']")WebDriverWait(driver, 10).until(EC.visibility_of_element_located(locator))ele = driver.find_element(*locator)time.sleep(3)# 2. 使用js进行滚动操作# 移动元素element对象的“底端”与当前窗口的“底部”对齐driver.execute_script("arguments[0].scrollIntoView(false);", ele)time.sleep(3)# 移动元素element对象的“顶端”与当前窗口的“顶部”对齐driver.execute_script("arguments[0].scrollIntoView();", ele)time.sleep(3)# 移动到页面顶部driver.execute_script("window.scrollTo(document.body.scrollHeight, 0)")time.sleep(3)# 移动到页面底部driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")time.sleep(3)#将滚动条移动到页面的顶部driver.execute_script("var q=document.documentElement.scrollTop=0")time.sleep(3)#将页面滚动条拖到底部driver.execute_script("var q=document.documentElement.scrollTop=10000")time.sleep(3)driver.quit()
3.2 日期选择
有些日期选择框为 readonly 属性,如 ,此时无法通过 send_keys 直接输入日期修改,此时就可以通过 js 代码修改 readonly 属性,并设置日期。
示例代码:
import timefrom selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.keys import Keysdriver = webdriver.Chrome()driver.get('https://www.12306.cn/index/')driver.maximize_window()WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, 'fromStationText')))driver.find_element_by_id('fromStationText').send_keys('北京', Keys.ENTER)time.sleep(1)driver.find_element_by_id('toStationText').send_keys('上海', Keys.ENTER)time.sleep(1)# js 语句修改出发日期为 2020-09-30,并点击查询js = "ele = document.getElementById('train_date');ele.readOnly= false;ele.value='2020-09-30';" \ "ele = document.getElementById('search_one');ele.click()"driver.execute_script(js)time.sleep(5)driver.quit()
更多用法请先学习 JS 教程
4文件上传 4.1 input 标签使用 send_keys如果文件上传的代码是使用 标签实现的,可以直接使用 send_keys
input_file.html 文件<html><head> <meta charset="UTF-8"> <title>Titletitle>head><body> <input type="file" name="file">body>html>
import timefrom selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECdriver = webdriver.Chrome()driver.get("http://localhost:63342/web_test/study/input_upfile.html")# 1. 如果是input可以直接输入路径的,直接用send_keyslocator = (By.NAME, "file")WebDriverWait(driver, 10).until(EC.visibility_of_element_located(locator))# 使用 send_keys 上传图片时必须使用绝对路径driver.find_element(*locator).send_keys("F:\\banner.jpg")time.sleep(5)driver.quit()
4.2 AutoIt工具
如果文件上传的代码不是使用 input标签实现的,则需要借助第三方工具(1. AutoIt工具 和 pyautoit库、2. SendKeys第三方库、3. Python pywin32库和spy++工具)
1.2.1 AutoIt 工具和 pyautoit库
1.2.2 SendKeys 库
1.2.3 pywin32 库和 spy++工具
AutoIt 工具下载地址:https://www.autoitscript.com/site/autoit/downloads/,AutoIt 工具这里不做介绍,大家可自行百度。

链接:https://www.cnblogs.com/sharef/p/13635702.html
本文为51Testing经授权转载,转载文章所包含的文字来源于作者。如因内容或版权等问题,请联系51Testing进行删除
推荐阅读点击阅读☞切换窗口时,用Selenium定位元素超方便
点击阅读☞用Selenium通过豆瓣的滑动验证码,超简单!
点击阅读☞随时可以调用公共方法的Selenium项目实战
点击阅读☞Selenium那些值得再次巩固的知识
点击阅读☞元素定位只会用Selenium?也太落后了吧!
