Webdriver之API详解(3)_自动化测试 前言 前两篇API链接 ①操作多选的选择列表 被测HTML代码


前言

前两篇API链接

①操作多选的选择列表

被测HTML代码


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>操作多选列表</title>
</head>
<body>
<select name="fruit" size="6" multiple=true>
<option id="peach" value="taozi">桃子</option>
<option id="watermelon" value="xigua">西瓜</option>
<option id="orange" value="juzi">橘子</option>
<option id="kiwifruit" value="nihoutao">猕猴桃</option>
<option id="maybush" value="shanzha">山楂</option>
<option id="litchi" value="lizhi">荔枝</option>
</select>
</body>
</html>


调用API实例代码

Webdriver之API详解(3)_selenium_02Webdriver之API详解(3)_html_03


 1     def testMultipleOptions(self):
2 from selenium.webdriver.support.ui import Select
3 import time
4 self.driver.get(r'file:///C:/Users/v-xug/Desktop/multipleOptions.html')
5 select_element = Select(self.driver.find_element_by_xpath('//select'))
6 # 通过序号选择第一个元素
7 select_element.select_by_index(0)
8 # 通过文本选择山楂
9 select_element.select_by_visible_text('山楂')
10 # 通过选项的value属性值选择value=猕猴桃
11 select_element.select_by_value('nihoutao')
12 # 打印所有选中文本
13 for option in select_element.all_selected_options:
14 print(option.text)
15 # 再次选中3个选项
16 select_element.select_by_index(1)
17 select_element.select_by_value('juzi')
18 select_element.select_by_visible_text('荔枝')
19 # 取消3个选项
20 select_element.deselect_by_index(0)
21 select_element.deselect_by_value('nihoutao')
22 select_element.deselect_by_visible_text('山楂')

实例代码

Webdriver之API详解(3)_selenium_02Webdriver之API详解(3)_html_03


 1 桃子
2 猕猴桃
3 山楂
4 .
5 ----------------------------------------------------------------------
6 Ran 1 test in 10.988s
7
8 OK
9
10 Process finished with exit code 0

输出

说明

运行这段代码看到的效果是,先选择3个选项并打印被选择的选项的文本值,再次选中3个选项并取消之前被选中的3个选项,对于可以多选的操作列表,上面的几个方法是很实用的,当然实际中可能遇见各种不同的情况,还需多积累经验对不同问题用不同方法。

②操作可以输入的下拉列表(输入的同时模拟按键)

被测HTML代码


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>操作可输入下拉列表,输入的同时模拟按键</title>
</head>
<body>
<div style="position:relative;">
<input list="pasta" id="select">
<datalist id="pasta">
<option>Bavette</option>
<option>Rigatoni</option>
<option>Fiorentine</option>
<option>Gnocchi</option>
<option>Tagliatelle</option>
<option>Penne lisce</option>
<option>Pici</option>
<option>Pappardelle</option>
<option>Spaghetti</option>
<option>Cannelloni</option>
<option>Cancl</option>
</datalist>
</div>
</body>
</html>


调用API实例代码

Webdriver之API详解(3)_selenium_02Webdriver之API详解(3)_html_03


 1     def testInputSelect(self):
2 self.driver.get(r'file:///C:/Users/v-xug/Desktop/inputselect.html')
3 from selenium.webdriver.common.keys import Keys
4 inputselect = self.driver.find_element_by_id('select')
5 inputselect.clear()
6 import time
7 time.sleep(1)
8 # 输入的同时按下箭头键
9 inputselect.send_keys('c', Keys.ARROW_DOWN)
10 time.sleep(1)
11 inputselect.send_keys(Keys.ARROW_DOWN)
12 time.sleep(1)
13 inputselect.send_keys(Keys.ENTER)
14 time.sleep(3)

操作可输入下拉列表

说明

运行这段代码可以看到输入框输入c的同时下拉选项会筛选出数据,且选中筛选出的第一项,但是在某些浏览器中不会看到效果(我写完运行时看到的效果就没有)。keys模块提供了很多其他的模拟按键,可以通过dir()查看Keys的功能

③操作单选框

被测HTML代码


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>操作单选框</title>
</head>
<body>
<form>
<input type="radio" name="fruit" value="berry" />草莓</input>
<br />
<input type="radio" name="fruit" value="watermelon" />西瓜</input>
<br />
<input type="radio" name="fruit" value="orange" />橙子</input>
</form>
</body>
</html>


调用API实例代码

Webdriver之API详解(3)_selenium_02Webdriver之API详解(3)_html_03


 1     def testRadio(self):
2 import time
3 self.driver.get(r'file:///C:/Users/v-xug/Desktop/radio.html')
4 # 定位到草莓选项
5 time.sleep(2)
6 berry = self.driver.find_element_by_xpath("//input[@value='berry']")
7 berry.click()
8 # 断言是否被选中
9 self.assertTrue(berry.is_selected())
10
11 if berry.is_selected():
12 # 如果被选中了重新选择西瓜选项
13 watermelon = self.driver.find_element_by_xpath("//input[@value='watermelon']")
14 watermelon.click()
15 # 断言草莓未被选中
16 self.assertFalse(berry.is_selected())
17 # 查找所有的选项
18 options = self.driver.find_elements_by_xpath("//input[@name='fruit']")
19 # 遍历所有的选项,如果找到orange且未被选中,那么就选中这项
20 for option in options:
21 if option.get_attribute('value')=='orange':
22 if not option.is_selected():
23 option.click()

实例代码

④操作复选框

被测HTML代码


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>操作复选框</title>
</head>
<body>
<form name="form1">
<input type="checkbox" name="fruit" value="berry" />草莓</input>
<input type="checkbox" name="fruit" value="watermelon" />西瓜</input>
<input type="checkbox" name="fruit" value="orange" />橙子</input>
</form>
</body>
</html>


调用API实例代码

Webdriver之API详解(3)_selenium_02Webdriver之API详解(3)_html_03


 1     def testCheckBox(self):
2 self.driver.get(r'file:///C:/Users/v-xug/Desktop/checkbox.html')
3 # 选中一个选项并取消
4 berry = self.driver.find_element_by_xpath("//input[@value='berry']")
5 berry.click()
6 # 断言是否被选中
7 self.assertTrue(berry.is_selected())
8 # 取消选中
9 if berry.is_selected():
10 berry.click()
11 # 遍历所有的选项并选中所有的选项
12 options = self.driver.find_elements_by_xpath("//input[@name='fruit']")
13 for option in options:
14 if not option.is_selected():
15 option.click()

实例代码

⑤断言页面源码中的关键字

被测地址

http://www.baidu.com">​http://www.baidu.com​

Webdriver之API详解(3)_selenium_02Webdriver之API详解(3)_html_03


1     def testAssertIn(self):
2 self.driver.get('http://www.baidu.com')
3 self.driver.find_element_by_id('kw').send_keys('linux超')
4 self.driver.find_element_by_id('su').click()
5 import time
6 time.sleep(4)
7 self.assertIn('linux超', self.driver.page_source, msg='页面源码中不存在该关键字')

实例代码

说明

有时候会出现页面存在要断言的关键字,但是结果仍然断言失败, 这有可能是由于页面没有加载完全就开始断言语句, 导致要断言的内容在页面源码中找不到。

⑥对当前浏览器窗口截屏

被测地址

http://www.baidu.com">​http://www.baidu.com​

Webdriver之API详解(3)_selenium_02Webdriver之API详解(3)_html_03


1     def testScreenShot(self):
2 self.driver.get('http://www.baidu.com')
3 try:
4 # 使用get_screenshot_as_file(filename)方法,对浏览器当前打开的页面截图,并保存在当前目录下
5 self.driver.get_screenshot_as_file('baidu.png')
6 except IOError as e:
7 print(e)

实例代码

截图

Webdriver之API详解(3)_python_16

说明

调用截屏函数get_screenshot_as_file()截图成功后会返回True,如果发生了IOError异常,会返回False。函数中传递的参数可以是绝对路径也可以是相对路径;当自动化测试过程中,未实现预期结果,可以将页面截图保存,方便更快速地定位问题。

⑦拖拽页面元素

被测地址

http://jqueryui.com/resources/demos/draggable/scroll.html">​http://jqueryui.com/resources/demos/draggable/scroll.html​

调用API实例代码

Webdriver之API详解(3)_selenium_02Webdriver之API详解(3)_html_03


 1     def testDragDrop(self):
2 import time
3 self.driver.get(r'http://jqueryui.com/resources/demos/draggable/scroll.html')
4 element1 = self.driver.find_element_by_id('draggable')
5 element2 = self.driver.find_element_by_id('draggable2')
6 element3 = self.driver.find_element_by_id('draggable3')
7 from selenium.webdriver import ActionChains
8 action = ActionChains(self.driver)
9 # 把第一个元素拖拽到第二个元素的位置
10 action.drag_and_drop(element1, element2).perform()
11 # 把第三个元素拖拽10个像素,拖拽2次
12 for i in range(2):
13 action.drag_and_drop_by_offset(element3,10,10).perform()
14 time.sleep(2)
15 action.release()

实例代码

说明:ActionChains模块在前面已经涉及到过了,所有的和鼠标操作有关的动作都需要使用此模块模拟

⑧模拟键盘单个按键操作

被测地址

http://www.sogou.com">​http://www.sogou.com​

调用API实例代码

Webdriver之API详解(3)_selenium_02Webdriver之API详解(3)_html_03


 1     def testSingleKey(self):
2 import time
3 self.driver.get('http://www.sogou.com')
4 query = self.driver.find_element_by_id('query')
5 # 导入模拟按键模块
6 from selenium.webdriver.common.keys import Keys
7 # 输入框发送一个f12按键
8 query.send_keys(Keys.F12)
9 time.sleep(2)
10 # 输入框中输入搜索内容并按下回车键
11 query.send_keys('selenium')
12 query.send_keys(Keys.ENTER)
13 time.sleep(2)

实例代码

说明

有些电脑运行这个代码可能看不到效果,因为有的电脑的F12键 是需要和Fn组合才能生效的。

总结

今天的整理到此结束,说实话我不知道对读到我文章的人帮助有多大,但是对我个人而言是又经历了一次知识的梳理。把之前忘记的也都慢慢的想起来了,虽然每个实例看着都挺简单的,其实耗费了我很多精力和时间,因为我想让读到我博客的人只看一次代码只运行一次实例就能知道实现的是什么功能,能把这个功能应用到复杂的测试场景中。其实这也是我自己的一次自我总结把!



----------------------------真正的勇士, 敢于直面惨淡的Warning、 敢于正视淋漓的Error--------------------------

版权声明


您的支持是对博主最大的鼓励,感谢您的认真阅读

本文版权归作者所有,欢迎,但未经作者同意必须保留此段声明, 且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

作者: Linux超