前置步骤:


  安装selenium,chrome驱动,Python3.6

学习目的:


  常见API的使用

涉及的API:


step1: 访问一个网址

step2: 网页的前进和后退

step3: 刷新当前页面

step4: 浏览器窗口最大化

step5: 获取并设置当前窗口的位置

step6: 获取并设置当前窗口的大小

step7: 获取页面的title属性值

step8: 获取页面HTML源代码

step9: 获取当前页面的URL

step10: 获取与切换浏览器窗口句柄

step11:获取页面元素的基本信息

step12: 获取页面元素的文本内容

step13: 判断页面元素是否可见

step14: 判断页面元素是否可操作

step15: 获取页面元素的属性

step16: 获取页面的CSS属性值

step17: 情况输入框中的内容

step18: 在输入框中输入指定的内容

step19: 单击按钮

step20: 双击某个元素

step21: 操作单选下拉列表

step22: 断言单选列表选项值

step23: 操作多选的选择列表

step24: 操作可以输入的下拉列表

正式步骤:


  测试使用的unittest框架代码如下:

  

# -*-  coding:utf-8 -*-
from selenium import webdriver
import unittest class WebdriverAPI(unittest.TestCase):
def setUp(self):
# 每个用例都执行,在单个用例运行前执行
#打开浏览器
self.driver = webdriver.Chrome() def tearDown(self):
#每个用例都执行,在单个用例运行后执行
#退出浏览器
self.driver.quit() def test_visitURL(self):
#测试步骤
pass if __name__ == '__main__':
unittest.main()

step1: 访问一个网址

    def test_visitURL(self):
url = 'https://www.baidu.com/'
self.driver.get(url)
assert self.driver.title == '百度一下,你就知道'
print(self.driver.title)

step2: 网页的前进和后退

    def test_visitHistoryPage(self):
firstUrl = 'https://www.baidu.com/'
secondUrl = 'https://cn.bing.com/'
#访问第一个网址
self.driver.get(firstUrl)
time.sleep(2)
#访问第二个网址
self.driver.get(secondUrl)
time.sleep(2)
#回到百度网址
self.driver.back()
time.sleep(2)
#再回到bing
self.driver.forward()
time.sleep(2)

step3: 刷新当前页面

self.driver.refresh()

step4: 浏览器窗口最大化

        #窗口最大化
self.driver.maximize_window()

step5: 获取并设置当前窗口的位置

        #获取浏览器位置
position = self.driver.get_window_position()
#打印出浏览器的坐标
print('当前窗口x轴%s,y轴%s'%(position['x'],position['y']))
#设置浏览器的位置
self.driver.set_window_position(x = 100,y = 100)
time.sleep(2)

step6: 获取并设置当前窗口的大小

        #获取浏览器窗口大小
window_size = self.driver.get_window_size()
print('current size %s :'%window_size)
print('宽%s,高%s'%(window_size['width'],window_size['height']))
#重新设置窗口大小
self.driver.set_window_size(width=1000,height=1000)

step7: 获取页面的title属性值

        #获取并打印页面title
page_title = self.driver.title
print(page_title)
#断言页面title内容
self.assertEqual(page_title,'百度一下,你就知道1','页面属性值错误')

step8: 获取页面HTML源代码

        #获取页面的HTML源代码
page_source = self.driver.page_source
print(page_source)
#断言源码是否包含关键字百度,显然会有回显信息打印出来,正确则没有回显msg
self.assertTrue('百度#'in page_source,'不包含')

step9: 获取当前页面的URL

        #获取当前页面url
self.driver.get(firstUrl)
correntPageUrl = self.driver.current_url
print(correntPageUrl)

step10: 获取与切换浏览器窗口句柄

        #获取与切换浏览器窗口句柄
self.driver.get(firstUrl)
#获取当前窗口的句柄
firstHander = self.driver.current_window_handle
#打印获取的第一个窗口句柄
print(firstHander)
#输入框输入检索内容:selenium
self.driver.find_element_by_id('kw').send_keys('selenium3')
#单击搜索按钮
self.driver.find_element_by_id('su').click()
#设置等待时间3s
time.sleep(3)
#单击需要点击的网页链接
self.driver.find_element_by_partial_link_text('Python+Selenium3最新配置 - CSDN博客').click()
time.sleep(3)
#获取所有的句柄
allHander = self.driver.window_handles
print('当前窗口句柄:'+ allHander[-1])
#获取所有窗口句柄
for hander in allHander:
print(hander)
#将操作句柄切换到当前窗口
self.driver.switch_to.window(allHander[-1])
time.sleep(3)
#可以关闭当前窗口,不然关闭的是firstUrl
self.driver.close()
time.sleep(3)
print(firstHander)
self.driver.switch_to.window(firstHander)
self.driver.find_element_by_id('kw').clear()
time.sleep(2)

step11:获取页面元素的基本信息

        firstUrl = 'https://www.baidu.com/'
self.driver.get(firstUrl)
testElement = self.driver.find_element_by_xpath("//a[text()='新闻']")
print(testElement.tag_name,testElement.size)

step12: 获取页面元素的文本内容

        testElementText = self.driver.find_element_by_xpath("//a[text()='新闻']")
print(testElementText.text)

step13: 判断页面元素是否可见

        testElementVisiable = self.driver.find_element_by_xpath("//a[text()='新闻']")
print(testElementVisiable.is_displayed())

step14: 判断页面元素是否可操作,是否已选

        testElementVisiable = self.driver.find_element_by_xpath("//a[text()='新闻']")
print(testElementVisiable.is_displayed())
#判断页面元素是否可操作
print(testElementVisiable.is_enabled())

step15: 获取页面元素的属性

        #获取页面元素属性
attribution = testElementVisiable.get_attribute('class')
print(attribution)
attribution1 = testElementVisiable.get_attribute('name')
print(attribution1)
attribution2 = testElementVisiable.get_attribute('text')
print(attribution2)

step16: 获取页面的CSS属性值

        css_property = self.driver.find_element_by_xpath("//a[text()='新闻']")
print(css_property.value_of_css_property('height'))
print(css_property.value_of_css_property('width'))
print(css_property.value_of_css_property('font-size'))

step17: 清空输入框中的内容

        self.driver.find_element_by_id('kw').send_keys('test')
self.driver.find_element_by_id('kw').clear()

step18: 在输入框中输入指定的内容

self.driver.find_element_by_id('kw').send_keys('test')

step19: 单击按钮

self.driver.find_element_by_partial_link_text('Python+Selenium3最新配置 - CSDN博客').click()

step20: 双击某个元素

        input = self.driver.find_element_by_id('kw')
input.send_keys('双击全选,背景高亮')
from selenium.webdriver import ActionChains
action_chains = ActionChains(self.driver)
#双击后,高亮两个字背景高亮,只是为了证明双击起效了
action_chains.double_click(input).perform()
time.sleep(3)

step21: 操作单选下拉列表

测试用下拉html页面代码

<html>
<body>
<form>
<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
</form>
</body>
</html>

测试脚本:

        #操作简单的下拉列表
url = 'F:\\python_stack\\python_autotest\\webdriver_api\\xiala.html'
self.driver.get(url)
all_options = self.driver.find_elements_by_tag_name('option')
for option in all_options:
print(option.text)
print(option.get_attribute('value'))
option.click()
time.sleep(1)

step22: 断言单选列表选项值

        url = 'F:\\python_stack\\python_autotest\\webdriver_api\\xiala.html'
self.driver.get(url)
from selenium.webdriver.support.ui import Select
select_element = Select(self.driver.find_element_by_name('cars'))
current_options = select_element.options
current_optionsList = []
#遍历options,并生成option文本值的列表
for option in current_options:
print(option.text)
current_optionsList.append(option.text) print(current_optionsList)
expect_optionsList = ['Volvo','Saab','Fiat','Audi']
#断言两个列表是否相同
self.assertListEqual(current_optionsList,expect_optionsList)

step23: 操作多选的选择列表

        url = 'F:\\python_stack\\python_autotest\\webdriver_api\\xiala.html'
self.driver.get(url)
from selenium.webdriver.support.ui import Select
select_element = Select(self.driver.find_element_by_name('cars'))
select_element.select_by_index(0)
select_element.select_by_visible_text('Fiat')
select_element.select_by_value('audi')
time.sleep(3)
#取消选中的单位
select_element.deselect_all()
time.sleep(3)

step24: 操作可以输入的下拉列表

测试用HTML代码

<!DOCTYPE html>
<html>
<body>
<div style="position: relative;">
<input list="pasta" id = "select">
<datalist id ="pasta">
<option>C</option>
<option>Java</option>
<option>Python</option>
<option>C#</option>
<option>Ruby</option>
</datalist>
</div>
</body>
</html>

测试脚本:

        #带输入的下拉列表操作
url = 'F:\\python_stack\\python_autotest\\webdriver_api\\data.html'
self.driver.get(url)
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
self.driver.find_element_by_id("select").clear()
time.sleep(1)
#输入的同时向下按箭头
self.driver.find_element_by_id("select").send_keys("Java",Keys.ARROW_DOWN)
time.sleep(2)
self.driver.find_element_by_id("select").send_keys(Keys.ENTER)
time.sleep(2)

难点分析:


API看似简单,实际的简单应用中,还是花了很多时间去实际运行,眼高手低不好

学习总结:


还有24个常用API,下班后继续

参考资料:

参考英文官方资料:http://selenium-python.readthedocs.io/locating-elements.html

Python3 Selenium自动化web测试 ==> 第三节 常用WebDriver API使用示例上(24个API)的更多相关文章

  1. Python3 Selenium自动化web测试 ==> 第七节 WebDriver高级应用 -- 浮动框中,单击选择某个关键字选项

    学习目的: 了解WebDriver的高级应用 正式步骤: 测试Python3代码 # -*- coding:utf-8 -*- from selenium import webdriver from ...

  2. Python3 Selenium自动化web测试 ==> 第六节 WebDriver高级应用 -- 操作web页面的滚动条

    学习目的: 掌握页面元素定位以外的其他重要知识点. 正式步骤: 测试Python3代码 # -*- coding:utf-8 -*- from selenium import webdriver fr ...

  3. Python3 Selenium自动化web测试 ==> 第二节 页面元素的定位方法 <上>

    前置步骤: 上一篇的Python单元测试框架unittest,我认为相当于功能测试测试用例设计中的用例模板,在自动化用例的设计过程中,可以封装一个模板,在新建用例的时候,把需要测试的步骤添加上去即可: ...

  4. Python3 Selenium自动化web测试 ==> 第五节 WebDriver高级应用 -- 使用JavaScript操作页面元素

    学习目的: 中级水平技术提升 在WebDriver脚本代码中执行JS代码,可以解决某些 .click()方法无法生效等问题 正式步骤: Python3代码如下 # -*- coding:utf-8 - ...

  5. Python3 Selenium自动化web测试 ==> 第十一节 WebDriver高级应用 -- 显示等待 + 二次封装

    学习目的: 掌握显示等待 掌握二次封装 正式步骤: step1:显示等待的代码示例 # -*- coding:utf-8 -*- from selenium import webdriver from ...

  6. Python3 Selenium自动化web测试 ==> 第十节 WebDriver高级应用 -- xpath语法

    学习目的: xpath定位是针对常规定位方法中,最有效的定位方式. 场景: 页面元素的定位. 正式步骤: step1:常规属性 示例UI 示例UI相关HTML代码 相关代码示例: #通过id定位 dr ...

  7. Python3 Selenium自动化web测试 ==> 第一节 起始点之Python单元测试框架 unittest

    前置步骤 Python版本:3.6.4 selenium版本:3.11.0 >>> import selenium >>> help(selenium) IDE:P ...

  8. Python3 Selenium自动化web测试 ==> 第九节 WebDriver高级应用 -- 操作select 和 alert

    学习目的: 掌握页面常规元素的定位方法 场景: 网页正常的select元素下拉框常规方法和select专属方法 正式步骤: step1:常规思路select页面元素定位 处理HTML代码截图 # -* ...

  9. Python3 Selenium自动化web测试 ==>FAQ:Unittest测试报告生成文件名加测试完成时间字符串

    测试代码,虽然有点笨重,以后再修改: if __name__ == '__main__': report = os.path.join('D:/Python36/report/report.html' ...

随机推荐

  1. Mybatis关联查询(转载)

    原文地址: http://www.cnblogs.com/xiaolang8762400/p/7399892.html   mybatis 提供了高级的关联查询功能,可以很方便地将数据库获取的结果集映 ...

  2. App支付宝登录授权

    一.在支付宝开放平台申请App应用,并且配置后台信息 https://openhome.alipay.com/platform/appManage.htm#/apps 填写自己的申请信息 添加应用功能 ...

  3. ESP8266烧录选项中的QIO 和 DIO解释

    https://blog.csdn.net/recclay/article/details/78956580 看到的由烧录引起的QIO和DIO问题探索.. 所以一般选择DIO QIO -> Qu ...

  4. SpringMVC之RequestContextHolder分析(转)

    链接:https://blog.csdn.net/zzy7075/article/details/53559902

  5. @Value和@ConfigurationProperties

    1.@Value用法 https://blog.csdn.net/u010832551/article/details/73826914 2.@ConfigurationProperties用法 ht ...

  6. 《Learning Structured Representation for Text Classification via Reinforcement Learning》论文翻译.md

    摘要 表征学习是自然语言处理中的一个基本问题.本文研究了如何学习文本分类的结构化表示.与大多数既不使用结构又依赖于预先指定结构的现有表示模型不同,我们提出了一种强化学习(RL)方法,通过自动覆盖优化结 ...

  7. 题解 [HNOI2004]宠物收养场

    解析 这题似乎是裸的平衡树\(+\)模拟...于是用\(treap\)写了个板子. 看上去,我们似乎要维护两颗树(宠物和顾客), 然而,注意到,同一时间宠物点只有一类人(或物qwq), 所以,只要判断 ...

  8. 网络编程-UDP echo server

    1. UDP简介 UDP 和TCP 的区别包括 1. 面向字节流和面向报文 2. TCP必须要建立连接后才能进行数据交换,但是UDP则并没有连接的建立和释放过程.面向字节流说明,tcp报文段(segm ...

  9. 二进制上的数位dpPOJ 3252

    Round number POJ - 3252 题目大意:一个"round number" 数的定义是,将它转化成2进制后,0的个数大于等于1的个数,要求的就是在[s,f]范围内& ...

  10. BZOJ 4034 [HAOI2015]树上操作 线段树+树剖或dfs

    题意 直接照搬原题面 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中所 ...