Selenium2+Python2.0自动化测试【8】 下拉框
来源:互联网 发布:java A 编辑:程序博客网 时间:2024/06/01 08:50
操作下拉框
1. 定位select标签下拉框
什么是select标签下拉菜单,打开百度-高级设置界面,如下图所示
Select提供了三种选择方法:
select_by_index(index) ——通过选项的顺序,第一个为 0
select_by_value(value) ——通过value属性
select_by_visible_text(text) ——通过选项可见文本
先要导入select方法:
from selenium.webdriver.support.select import Select
# coding:utf-8# select下拉框from selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsfrom selenium.webdriver.support.ui import Selectimport timedriver = webdriver.Firefox()driver.implicitly_wait(10)driver.get("http://www.baidu.com")# 打开百度搜索高级设置页面move = driver.find_element_by_xpath(".//*[@id='u1']/a[8]")ActionChains(driver).move_to_element(move).perform()driver.find_element_by_xpath(".//*[@id='wrapper']/div[6]/a[2]").click()# 通过索引s1 = driver.find_element_by_xpath(".//*[@id='adv-setting-4']/select")Select(s1).select_by_index(1)# 通过values2 = driver.find_element_by_xpath(".//*[@id='adv-setting-5']/select")Select(s2).select_by_value("pdf")# 通过texts1 = driver.find_element_by_xpath(".//*[@id='adv-setting-4']/select")Select(s1).select_by_visible_text("最近一年")driver.find_element_by_xpath(".//*[@id='adv-setting-7']/input[1]").send_keys("python")driver.quit()
也可直接定位
# coding:utf-8# select下拉框直接定位from selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsimport timedriver = webdriver.Firefox()driver.implicitly_wait(10)driver.get("http://www.baidu.com")# 打开百度搜索高级设置页面move = driver.find_element_by_xpath(".//*[@id='u1']/a[8]")ActionChains(driver).move_to_element(move).perform()driver.find_element_by_xpath(".//*[@id='wrapper']/div[6]/a[2]").click()# 直接定位下拉框菜单项driver.find_element_by_xpath(".//*[@id='adv-setting-4']/select/option[4]").click()time.sleep(3)driver.find_element_by_xpath(".//*[@id='adv-setting-7']/input[1]").send_keys("python")driver.quit()
2. 定位非select标签下拉框
什么是非select标签下拉框,如下图所示:58同城城市选择下拉框
分两步
1、先定位下拉框并点击展开菜单元素
2、在选择下拉框菜单项
# coding=utf-8# 下拉框from selenium import webdriverimport timedriver = webdriver.Firefox()driver.maximize_window()driver.get("http://cd.58.com/")# 进入城市选择页面driver.find_element_by_xpath(".//*[@id='commonTopbar_ipconfig']/a[1]").click()# 定位下拉框并展开driver.find_element_by_id("selector-province-result").click()# 再对下拉菜单中的选项选择driver.find_element_by_xpath(".//*[@id='selector-province-box']/li[7]").click()time.sleep(3)driver.quit()
阅读全文
0 0
- Selenium2+Python2.0自动化测试【8】 下拉框
- Selenium2+Python2.0自动化测试【10】 复选框
- Selenium2+Python2.0自动化测试【9】 单选框
- Selenium2+Python2.0自动化测试【15】 文本框
- Selenium2+Python2.0自动化测试【1】 环境搭建
- Selenium2+Python2.0自动化测试【2】 操作浏览器
- Selenium2+Python2.0自动化测试【3】 元素定位
- Selenium2+Python2.0自动化测试【4】 元素基本操作
- Selenium2+Python2.0自动化测试【5】 鼠标事件
- Selenium2+Python2.0自动化测试【6】 键盘事件
- Selenium2+Python2.0自动化测试【7】 多窗口切换
- Selenium2+Python2.0自动化测试【11】 操作弹窗
- Selenium2+Python2.0自动化测试【12】 多表单切换
- Selenium2+Python2.0自动化测试【13】 操作滚动条
- Selenium2+Python2.0自动化测试【14】 上传文件
- Selenium2+Python2.0自动化测试【16】 日历控件
- Selenium2+Python2.0自动化测试【17】 获取元素属性
- Selenium2+Python2.0自动化测试【18】 unittest框架①
- 程序员笑话...
- 发送短信验证码的思路
- 1024. 科学计数法 (20)
- 递归组合
- Class.forName(String className)使用哪个类加载器? 2015-01-08 20:56 2196人阅读 评论(0) 收藏 举报 分类: Java虚拟机(13) 版权
- Selenium2+Python2.0自动化测试【8】 下拉框
- 学习方法推荐——时间管理之GTD方法集合论
- 微信公众号增粉秘籍:三大矩阵简介
- Html Table 合并单元格
- JaVA AWT 一个很好的计算器
- RK3288[Android6.0] Wifi相关文件及类整理
- MarkdownPad编写符号
- 欢迎使用CSDN-markdown编辑器
- proto生成shell