12.Selenium2 自动化测试实战-基于Python语言-定位一组元素
2017-04-01 14:44
387 查看
定位一组元素的方法与定位单个元素的方法类似,唯一的区别就是在单词element后面多加了个s表示复数。定位一组元素多用于以下场景:
*批量操作元素
*先获取一组元素,再从这组元素对象中过滤出需要操作的元素。
1.
from selenium import webdriver
import os,time
driver =webdriver.Chrome()
file_path='file:///'+os.path.abspath('./webdriver_api/web_page/checkbox.html')
driver.get(file_path)
#选择页面上所有tag name 为input 的元素
inputs =driver.find_elements_by_tag_name('input')
#然后从中过滤出type 为checkbox的元素,单击勾选
for i in inputs:
if i.get_attribute('type')=='checkbox':
i.click()
time.sleep(1)
driver.quit()
2.使用XPath或CSS来直接判断属性值,从而进行单击操作。
from selenium import webdriver
import os,time
driver =webdriver.Chrome()
file_path='file:///'+os.path.abspath('./webdriver_api/web_page/checkbox.html')
driver.get(file_path)
#通过XPath找到type=checkbox 的元素
checkboxes =driver.find_elements_by_xpath("//input[@type='checkbox']")
#通过CSS找到type=checkbox的元素
checkboxes =driver.find_elements_by_css_selector('input[type=checkbox]')
for checkbox in checkboxes:
checkbox.click()
time.sleep(1)
#打印当前页面上type为checkbox的个数
print(len(checkboxes))
#把页面上最后一个checkbox的钩给去掉
driver.find_elements_by_css_selector('input[type=checkbox]').pop().click()
time.sleep(2)
driver.quit()
*批量操作元素
*先获取一组元素,再从这组元素对象中过滤出需要操作的元素。
1.
from selenium import webdriver
import os,time
driver =webdriver.Chrome()
file_path='file:///'+os.path.abspath('./webdriver_api/web_page/checkbox.html')
driver.get(file_path)
#选择页面上所有tag name 为input 的元素
inputs =driver.find_elements_by_tag_name('input')
#然后从中过滤出type 为checkbox的元素,单击勾选
for i in inputs:
if i.get_attribute('type')=='checkbox':
i.click()
time.sleep(1)
driver.quit()
2.使用XPath或CSS来直接判断属性值,从而进行单击操作。
from selenium import webdriver
import os,time
driver =webdriver.Chrome()
file_path='file:///'+os.path.abspath('./webdriver_api/web_page/checkbox.html')
driver.get(file_path)
#通过XPath找到type=checkbox 的元素
checkboxes =driver.find_elements_by_xpath("//input[@type='checkbox']")
#通过CSS找到type=checkbox的元素
checkboxes =driver.find_elements_by_css_selector('input[type=checkbox]')
for checkbox in checkboxes:
checkbox.click()
time.sleep(1)
#打印当前页面上type为checkbox的个数
print(len(checkboxes))
#把页面上最后一个checkbox的钩给去掉
driver.find_elements_by_css_selector('input[type=checkbox]').pop().click()
time.sleep(2)
driver.quit()
相关文章推荐
- python-day0003-搭建基于python的selenium自动化测试---元素定位
- 15.Selenium2 自动化测试实战-基于Python语言-警告框处理
- 17.Selenium2 自动化测试实战-基于Python语言-操作cookie
- 8.Selenium2 自动化测试实战-基于Python语言-WebElement接口2
- 13.Selenium2 自动化测试实战-基于Python语言-多表单切换
- 9.Selenium2 自动化测试实战-基于Python语言-键盘事件
- 20.Selenium2 自动化测试实战-基于Python语言-函数
- 6.Selenium2 自动化测试实战-基于Python语言-126邮箱登陆、获得验证信息
- 2.Selenium2 自动化测试实战-基于Python语言-类和方法
- 14.Selenium2 自动化测试实战-基于Python语言-多窗口切换
- 4. Selenium2 自动化测试实战-基于Python语言-跨目录模块调用
- 5.Selenium2 自动化测试实战-基于Python语言-控制浏览器后退、前进
- 18.Selenium2 自动化测试实战-基于Python语言-验证码处理
- 11.Selenium2 自动化测试实战-基于Python语言-隐式等待
- 19.Selenium2 自动化测试实战-基于Python语言-数组与字典
- 10.Selenium2 自动化测试实战-基于Python语言-显示等待
- 7.Selenium2 自动化测试实战-基于Python语言-WebElement接口1
- 1.Selenium2 自动化测试实战-基于Python语言-设置窗口大小、调用JS调整滚动条、截取图片
- 3.Selenium2 自动化测试实战-基于Python语言-模块调用
- 16.Selenium2 自动化测试实战-基于Python语言-上传文件