正文
Python3 Selenium自动化web测试 ==> 第九节 WebDriver高级应用 -- 操作select 和 alert
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
学习目的:
掌握页面常规元素的定位方法
场景:
网页正常的select元素下拉框常规方法和select专属方法
正式步骤:
step1:常规思路select页面元素定位
处理HTML代码截图
# -*- coding:utf-8 -*-
from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
#常规select方法:百度首页设置搜索结果显示条目数
url = "https://www.baidu.com"
dr = webdriver.Chrome()
dr.get(url)
# dr.find_element_by_link_text('设置').click()
# dr.find_element_by_class_name("setpref").click()
# time.sleep(1)
# dr.find_element_by_xpath('//*[@id="nr"]/option[3]').click()
#常规方法二:鼠标移动悬浮到设置按钮上点击搜索设置
mouse = dr.find_element_by_link_text('设置')
ActionChains(dr).move_to_element(mouse).perform()
dr.find_element_by_class_name("setpref").click()
time.sleep(1)
dr.find_element_by_xpath('//*[@id="nr"]/option[3]').click() time.sleep(3)
dr.close()#close是退出当前窗口,quit是退出整个dr进程
step2:Select专有方法,即selenium内置方法
处理HTML代码截图
解决方案:
# -*- coding:utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
import time url = 'https://www.baidu.com'
dr = webdriver.Chrome()
dr.get(url)
dr.find_element_by_link_text('设置').click()
dr.find_element_by_class_name("setpref").click()
time.sleep(1)
#method_1
s1 = dr.find_element_by_id('nr')
Select(s1).select_by_visible_text('每页显示50条')
time.sleep(3) #method_2
s2 = dr.find_element_by_id('nr')
Select(s2).select_by_value('')
time.sleep(3) #method_3
s3 = dr.find_element_by_id('nr')
Select(s3).select_by_index('')
time.sleep(1) dr.quit()
step3: 处理alert弹出框
处理情景截图,需要点击“确定”按钮,但此按钮无相应的html元素
解决方案:
# -*- coding:utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
import time
"""
如果弹出有弹出框需要你输入内容,那么就要使用定位元素后的send_keys()来输入内容
弹出框的确定可以按F12来查看页面元素,如果有对应的HTML元素,则正常定位,但是需要切换句柄才能操作
"""
url = 'https://www.baidu.com'
dr = webdriver.Chrome()
dr.get(url)
dr.find_element_by_link_text('设置').click()
dr.find_element_by_class_name("setpref").click()
time.sleep(1) #此步骤必须加等待时间
dr.find_element_by_link_text("保存设置").click()
# time.sleep(1)
test_alert = dr.switch_to.alert() # 切换到alert
def alert_display():
try:
t = test_alert.text
print(t)
return True
except:
return False
if alert_display():
test_alert.accept() #accept表示点击确定
test_alert.dismiss()#dismiss表示不接受弹出框
else:
print("alert未弹出") dr.quit()
难点分析:
初次使用的时候,需要多看内置方法
学习总结:
需要多操作,建立一个常用操作库参考
Python3 Selenium自动化web测试 ==> 第九节 WebDriver高级应用 -- 操作select 和 alert的更多相关文章
-
Python3 Selenium自动化web测试 ==> 第八节 WebDriver高级应用 -- 结束Windows中浏览器的进程
学习目的: 掌握WebDriver的高级应用 正式步骤: # -*- coding:utf-8 -*- from selenium import webdriver from selenium.web ...
-
Python3 Selenium自动化web测试 ==> 第一节 起始点之Python单元测试框架 unittest
前置步骤 Python版本:3.6.4 selenium版本:3.11.0 >>> import selenium >>> help(selenium) IDE:P ...
-
Python3 Selenium自动化web测试 ==> 第二节 页面元素的定位方法 <上>
前置步骤: 上一篇的Python单元测试框架unittest,我认为相当于功能测试测试用例设计中的用例模板,在自动化用例的设计过程中,可以封装一个模板,在新建用例的时候,把需要测试的步骤添加上去即可: ...
-
Python3 Selenium自动化web测试 ==> 第二节 页面元素的定位方法 -- iframe专题 <下>
学习目的: 掌握iframe矿建的定位,因为前端的iframe框架页面元素信息,大多时候都会带有动态ID,无法重复定位. 场景: 1. iframe切换 查看iframe 切换iframe 多个ifr ...
-
Python3 Selenium自动化web测试 ==> 第七节 WebDriver高级应用 -- 浮动框中,单击选择某个关键字选项
学习目的: 了解WebDriver的高级应用 正式步骤: 测试Python3代码 # -*- coding:utf-8 -*- from selenium import webdriver from ...
-
Python3 Selenium自动化web测试 ==> 第六节 WebDriver高级应用 -- 操作web页面的滚动条
学习目的: 掌握页面元素定位以外的其他重要知识点. 正式步骤: 测试Python3代码 # -*- coding:utf-8 -*- from selenium import webdriver fr ...
-
Python3 Selenium自动化web测试 ==> 第五节 WebDriver高级应用 -- 使用JavaScript操作页面元素
学习目的: 中级水平技术提升 在WebDriver脚本代码中执行JS代码,可以解决某些 .click()方法无法生效等问题 正式步骤: Python3代码如下 # -*- coding:utf-8 - ...
-
Python3 Selenium自动化web测试 ==> 第三节 常用WebDriver API使用示例上(24个API)
前置步骤: 安装selenium,chrome驱动,Python3.6 学习目的: 常见API的使用 涉及的API: step1: 访问一个网址 step2: 网页的前进和后退 step3: 刷新当前 ...
-
Python3 Selenium自动化web测试 ==> 第十一节 WebDriver高级应用 -- 显示等待 + 二次封装
学习目的: 掌握显示等待 掌握二次封装 正式步骤: step1:显示等待的代码示例 # -*- coding:utf-8 -*- from selenium import webdriver from ...
随机推荐
-
find 命令和sed命令练习
1.查找/var目录下属主为root,且属组为mail的所有文件 find /var/ -user root -group mail 2.查找/var目录下不属于root.lp.gdm的所有文件 fi ...
-
Debug .NET Framework Source
1.Microsoft Reference Source 在线source http://referencesource.microsoft.com/#System.Data.Linq 可以下载离线s ...
-
Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block
Download dll: http://www.microsoft.com/en-us/download/confirmation.aspx?id=15104 http://www.cnblogs. ...
-
INLINE HOOK过简单驱动保护的理论知识和大概思路
这里的简单驱动保护就是简单的HOOK掉内核API的现象 找到被HOOK的函数的当前地址在此地址处先修改页面保护属性然后写入5个字节.5个字节就是一个简单的JMP指令.这里说一下JMP指令,如下: 00 ...
-
2019CCPC秦皇岛自我反省&部分题解
练了一年半了,第一次打CCPC,险些把队友坑了打铁,最后也是3题危险捡了块铜. 非常水的点双连通,我居然不相信自己去相信板子,唉,结果整来整去,本来半个小时能出的题,整到了3个小时,大失误呀,不然就可 ...
-
【CUDA 基础】6.3 重叠内和执行和数据传输
title: [CUDA 基础]6.3 重叠内和执行和数据传输 categories: - CUDA - Freshman tags: - 深度优先 - 广度优先 toc: true date: 20 ...
-
codeforces723E
One-Way Reform CodeForces - 723E There are n cities and m two-way roads in Berland, each road connec ...
-
Pytest学习笔记(一) 环境安装及入门
简介 pytest是python的一个单元测试框架,类似于unittest,相对unittest来说,pytest使用更简单,功能更强大. 安装 pip3 install -U pytest 查看版本 ...
-
AcWing:176. 装满的油箱(bfs + dijiskla思想)
有N个城市(编号0.1…N-1)和M条道路,构成一张无向图. 在每个城市里边都有一个加油站,不同的加油站的单位油价不一样. 现在你需要回答不超过100个问题,在每个问题中,请计算出一架油箱容量为C的车 ...
-
Robot Framework(七)创建用户关键字
2.6创建用户关键字 关键字表用于通过将现有关键字组合在一起来创建新的更高级别关键字.这些关键字称为用户关键字,以区别于 测试库中实现的最低级库关键字.创建用户关键字的语法与创建测试用例的语法非常接近 ...
学习目的: 掌握WebDriver的高级应用 正式步骤: # -*- coding:utf-8 -*- from selenium import webdriver from selenium.web ...
前置步骤 Python版本:3.6.4 selenium版本:3.11.0 >>> import selenium >>> help(selenium) IDE:P ...
前置步骤: 上一篇的Python单元测试框架unittest,我认为相当于功能测试测试用例设计中的用例模板,在自动化用例的设计过程中,可以封装一个模板,在新建用例的时候,把需要测试的步骤添加上去即可: ...
学习目的: 掌握iframe矿建的定位,因为前端的iframe框架页面元素信息,大多时候都会带有动态ID,无法重复定位. 场景: 1. iframe切换 查看iframe 切换iframe 多个ifr ...
学习目的: 了解WebDriver的高级应用 正式步骤: 测试Python3代码 # -*- coding:utf-8 -*- from selenium import webdriver from ...
学习目的: 掌握页面元素定位以外的其他重要知识点. 正式步骤: 测试Python3代码 # -*- coding:utf-8 -*- from selenium import webdriver fr ...
学习目的: 中级水平技术提升 在WebDriver脚本代码中执行JS代码,可以解决某些 .click()方法无法生效等问题 正式步骤: Python3代码如下 # -*- coding:utf-8 - ...
前置步骤: 安装selenium,chrome驱动,Python3.6 学习目的: 常见API的使用 涉及的API: step1: 访问一个网址 step2: 网页的前进和后退 step3: 刷新当前 ...
学习目的: 掌握显示等待 掌握二次封装 正式步骤: step1:显示等待的代码示例 # -*- coding:utf-8 -*- from selenium import webdriver from ...
-
find 命令和sed命令练习
1.查找/var目录下属主为root,且属组为mail的所有文件 find /var/ -user root -group mail 2.查找/var目录下不属于root.lp.gdm的所有文件 fi ...
-
Debug .NET Framework Source
1.Microsoft Reference Source 在线source http://referencesource.microsoft.com/#System.Data.Linq 可以下载离线s ...
-
Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block
Download dll: http://www.microsoft.com/en-us/download/confirmation.aspx?id=15104 http://www.cnblogs. ...
-
INLINE HOOK过简单驱动保护的理论知识和大概思路
这里的简单驱动保护就是简单的HOOK掉内核API的现象 找到被HOOK的函数的当前地址在此地址处先修改页面保护属性然后写入5个字节.5个字节就是一个简单的JMP指令.这里说一下JMP指令,如下: 00 ...
-
2019CCPC秦皇岛自我反省&部分题解
练了一年半了,第一次打CCPC,险些把队友坑了打铁,最后也是3题危险捡了块铜. 非常水的点双连通,我居然不相信自己去相信板子,唉,结果整来整去,本来半个小时能出的题,整到了3个小时,大失误呀,不然就可 ...
-
【CUDA 基础】6.3 重叠内和执行和数据传输
title: [CUDA 基础]6.3 重叠内和执行和数据传输 categories: - CUDA - Freshman tags: - 深度优先 - 广度优先 toc: true date: 20 ...
-
codeforces723E
One-Way Reform CodeForces - 723E There are n cities and m two-way roads in Berland, each road connec ...
-
Pytest学习笔记(一) 环境安装及入门
简介 pytest是python的一个单元测试框架,类似于unittest,相对unittest来说,pytest使用更简单,功能更强大. 安装 pip3 install -U pytest 查看版本 ...
-
AcWing:176. 装满的油箱(bfs + dijiskla思想)
有N个城市(编号0.1…N-1)和M条道路,构成一张无向图. 在每个城市里边都有一个加油站,不同的加油站的单位油价不一样. 现在你需要回答不超过100个问题,在每个问题中,请计算出一架油箱容量为C的车 ...
-
Robot Framework(七)创建用户关键字
2.6创建用户关键字 关键字表用于通过将现有关键字组合在一起来创建新的更高级别关键字.这些关键字称为用户关键字,以区别于 测试库中实现的最低级库关键字.创建用户关键字的语法与创建测试用例的语法非常接近 ...