uniquefu Python+Selenium学习--select
场景
在处理下拉框(select)的时候selenium给我们提供了一系列的便捷方法,我们只需要使用selenium.webdriver.support.select.Select类来稍微封装一下就好了。
下面是我们经常会用到的一些方法
- options: 返回下拉框里所有的选项
- all_selected_options: 返回所有选中的选项
- select_by_value(value): 通过option的value值来选择
- select_by_index(index) 通过option的顺序来选择
- select_by_visible_text(text): 通过option的text来选择
代码
s7.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form enctype="multipart/form-data">
<div> <textarea name="meno" >123456</textarea> <select name="city" >
<option value="beijing">北京</option>
<option value="shanghai">上海</option>
<option value="nanjing" selected="selected">南京</option>
<option value="chengdu">成都</option>
</select> <input type="submit" value="提交" />
<input type="reset" value="重置" />
</div>
</form>
</body>
</html>
select--tag.py
#!/usr/bin/env python
# -*- codinfg:utf-8 -*-
'''
@author: Jeff LEE
@file: select--tag.pay
@time: 2018-05-10 10:44
'''
from selenium import webdriver
from selenium.webdriver.support.select import Select
from time import sleep
import os if 'HTTP_PROXY'in os.environ:
del os.environ['HTTP_PROXY'] dr = webdriver.Firefox()
file_path ='file://'+ os.path.abspath('s7.html')
print (file_path)
dr.get(file_path) city_selector = Select(dr.find_element_by_tag_name('select')) # 返回所有的options
print(city_selector.options)
sleep(1) # 返回所有选中的options
print(city_selector.all_selected_options)
sleep(1) # 通过option的value值来选择上海
city_selector.select_by_value('shanghai')
sleep(2) # 通过index来选择,比如选择第4项
city_selector.select_by_index(3)
sleep(1) # 通过option的text来选择
city_selector.select_by_visible_text('北京') dr.quit()
uniquefu Python+Selenium学习--select的更多相关文章
- 【python+selenium学习】Python常见错误之:IndentationError: unexpected indent
初入python+selenium学习之路,总会遇到这样那样的问题.IndentationError: unexpected indent,这个坑我已经踏进数次了,索性记录下来.都知道Python对代 ...
- Python+Selenium学习--自动化测试模型
前言 一个自动化测试框架就是一个集成体系,在这一体系中包含测试功能的函数库.测试数据源.测试对象识别标准,以及种可重用的模块.自动化测试框架在发展的过程中经历了几个阶段,模块驱动测试.数据驱动测试.对 ...
- Python+Selenium学习笔记15 - 读取txt和csv文件
读取txt的内容并用百度查找搜索 1 # coding = utf-8 2 3 from selenium import webdriver 4 import time 5 6 # 打开浏览器 7 d ...
- Python+Selenium学习--自动生成HTML测试报告
前言 在脚本运行完成之后,除了在log.txt 文件看到运行日志外,我们更希望能生一张漂亮的测试报告来展示用例执行的结果. HTMLTestRunner 是Python 标准库的unit ...
- Python+Selenium学习--异常截图
前言 Webdriver 提供错误截图函数get_screenshot_as_file(),可以帮助我们跟踪bug,在脚本无法继续执行时候, get_screenshot_as_file()函数将截取 ...
- Python+Selenium学习--cookie处理
场景 有时候我们需要验证浏览器中是否存在某个cookie,因为基于真实的cookie 的测试是无法通过白盒和集成测试完成的.webdriver 可以读取.添加和删除cookie 信息.webdrive ...
- Python+Selenium学习--控制浏览器控制条
场景 有时候web 页面上的元素并非直接可见的,就算把浏览器最大化,我们依然需要拖动滚动条才能看到想要操作的元素,这个时候就要控制页面滚动条的拖动,但滚动条并非页面上的元素,可以借助JavaScrip ...
- Python+Selenium学习--分页处理
场景 我们在测试一个web 应用时,经常出现翻页的情况,下面介绍翻页场景 代码 #!/usr/bin/env python # -*- codinfg:utf-8 -*- ''' @author: J ...
- Python+Selenium学习--下拉框处理
场景 下拉框也是web 页面上非常常见的功能,webdriver 对于一般的下拉框处理起来也相当简单,要想定位下拉框中的内容,首先需要定位到下拉框:这样的二次定位 下拉框一般有以下两种方式: 鼠标移上 ...
随机推荐
- Spring Cloud (3)B Ribbon 负载均衡 IRule
package com.service.config; import com.netflix.loadbalancer.IRule;import com.netflix.loadbalancer.Ra ...
- Django 之单个mysql表使用
1. mysql 表使用的功能有: 增 删 改 查 这几大功能, 但是mysql 和django 对接之后,使用的语法不是原生的SQL语法. 2. 增功能的实现: User.objects.cre ...
- 什么是ThreadLocal
当使用ThreadLocal维护变量时,ThreadLocal为每个使用该变量的线程提供独立的变量副本,所以每一个线程都可以独立地改变自己的副本,而不会影响其它线程所对应的副本.ThreadLocal ...
- Django权限auth模块详解
转自:http://www.cnblogs.com/Finley/p/5575305.html 1,auth模块是Django提供的标准权限管理系统,可以提供用户身份认证,用户组和权限管理 2,aut ...
- leetcode ex3 找出穿过最多点的直线 Max Points on a Line
题目 https://oj.leetcode.com/problems/max-points-on-a-line/ 答案与分析 http://www.aiweibang.com/yuedu/18326 ...
- U3D MemoryProfiler
MemoryProfiler Unity 5.3a4 has a new very lowlevel memory profiler API. It can tell you which object ...
- tomcat8 web工程启动,登陆页面失败问题解决
编辑该文件context.xml 增加以下内容 <CookieProcessor className="org.apache.tomcat.util.http.LegacyCookie ...
- 使用arguments对象验证函数的参数是否合法
<script>function sum(arg1,arg2) //加法函数{ var realArgCount = arguments.length; //调用函数时传递的实参个数 va ...
- 【转】Unity网格合并_材质合并
原帖请戳:Unity网格合并_材质合并 写在前面: 从优化角度,Mesh需要合并. 从换装的角度(这里指的是换形状.换组成部件的换装,而不是挂点型的换装),都需要网格合并.材质合并.如果是人物的换装, ...
- poj2115-Looooops-(扩展欧几里得定理)
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions:33752 Accepted: 9832 Descri ...