场景

在处理下拉框(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的更多相关文章

  1. 【python+selenium学习】Python常见错误之:IndentationError: unexpected indent

    初入python+selenium学习之路,总会遇到这样那样的问题.IndentationError: unexpected indent,这个坑我已经踏进数次了,索性记录下来.都知道Python对代 ...

  2. Python+Selenium学习--自动化测试模型

    前言 一个自动化测试框架就是一个集成体系,在这一体系中包含测试功能的函数库.测试数据源.测试对象识别标准,以及种可重用的模块.自动化测试框架在发展的过程中经历了几个阶段,模块驱动测试.数据驱动测试.对 ...

  3. Python+Selenium学习笔记15 - 读取txt和csv文件

    读取txt的内容并用百度查找搜索 1 # coding = utf-8 2 3 from selenium import webdriver 4 import time 5 6 # 打开浏览器 7 d ...

  4. Python+Selenium学习--自动生成HTML测试报告

    前言 在脚本运行完成之后,除了在log.txt 文件看到运行日志外,我们更希望能生一张漂亮的测试报告来展示用例执行的结果.        HTMLTestRunner 是Python 标准库的unit ...

  5. Python+Selenium学习--异常截图

    前言 Webdriver 提供错误截图函数get_screenshot_as_file(),可以帮助我们跟踪bug,在脚本无法继续执行时候, get_screenshot_as_file()函数将截取 ...

  6. Python+Selenium学习--cookie处理

    场景 有时候我们需要验证浏览器中是否存在某个cookie,因为基于真实的cookie 的测试是无法通过白盒和集成测试完成的.webdriver 可以读取.添加和删除cookie 信息.webdrive ...

  7. Python+Selenium学习--控制浏览器控制条

    场景 有时候web 页面上的元素并非直接可见的,就算把浏览器最大化,我们依然需要拖动滚动条才能看到想要操作的元素,这个时候就要控制页面滚动条的拖动,但滚动条并非页面上的元素,可以借助JavaScrip ...

  8. Python+Selenium学习--分页处理

    场景 我们在测试一个web 应用时,经常出现翻页的情况,下面介绍翻页场景 代码 #!/usr/bin/env python # -*- codinfg:utf-8 -*- ''' @author: J ...

  9. Python+Selenium学习--下拉框处理

    场景 下拉框也是web 页面上非常常见的功能,webdriver 对于一般的下拉框处理起来也相当简单,要想定位下拉框中的内容,首先需要定位到下拉框:这样的二次定位 下拉框一般有以下两种方式: 鼠标移上 ...

随机推荐

  1. Kubernetes中pod创建流程

    转自:https://blog.csdn.net/yan234280533/article/details/72567261 Pod是Kubernetes中最基本的部署调度单元,可以包含contain ...

  2. mingw 设置python 设置git环境变量

    1.python路径设置: 安装python 比如目录:C:\Python27 假如mingw安装C盘根目录下的话,进入下面目录:C:\MinGW\msys\1.0\etc 找到 fstab 文件修改 ...

  3. 1047A_Little C Loves 3 I(构造)

    A. Little C Loves 3 I time limit per test 1 second memory limit per test 256 megabytes input standar ...

  4. Unity 平台依赖编译

    位置:unity文档-Manual-Scripting-Platform dependent compilation Property: Function: UNITY_EDITOR #define ...

  5. RH_KABI_RESERVE的使用

    struct mm_struct { .......... #if defined(__GENKSYMS__) || !defined(CONFIG_SPAPR_TCE_IOMMU) /* We're ...

  6. linux内核中的const成员是否可以修改?

    本文的基础知识:由于前半部分内容是转的,且不知道原文出处,没法给出原文地址,大家自行百度 const的实现机制 const究竟是如何实现的呢?对于声明为const的内置类型,例如int,short,l ...

  7. Examples: How to Pronounce T

    Examples: How to Pronounce T Share Tweet Share Tagged With: Flap T, Stop T The [t] sound is not alwa ...

  8. WebStorm新创建项目介绍

    WebStorm创建一个项目 这里支持有很多的类型项目: Empty Project         ----一个空的项目 Html5 Boilerplate     ----HTML5开发框架 We ...

  9. oracle合并语句

    在sql server中的合并语句可以用xml path 详见http://www.cnblogs.com/codeyu/archive/2010/05/25/1743474.html 而oracle ...

  10. 学习JS的心路历程-参数传递方式(上)

    很多人认为JS的传递方式是值是Call by value, 物件及数组是Call by Reference.甚至还有人宣称其实JS是Call by sharing,那到底是哪一个呢? 这两天我们一一来 ...