Python3.x:遍历select下拉框获取value值

Select提供了三种选择方法:

# 通过选项的顺序,第一个为 0
select_by_index(index)
# 通过value属性
select_by_value(value)
# 通过选项可见文本
select_by_visible_text(text)

Select提供了四种方法取消选择:

deselect_by_index(index)
deselect_by_value(value)
deselect_by_visible_text(text)
deselect_all()

Select提供了三个属性方法给我们必要的信息:

# 提供所有的选项的列表,其中都是选项的WebElement元素
options
# 提供所有被选中的选项的列表,其中也均为选项
all_selected_options的WebElement元素
# 提供第一个被选中的选项,也是下拉框的默认值
first_selected_option

示例一:代码(selenium遍历select选项列表):

from selenium import webdriver

driver = webdriver.PhantomJS()
driver.get("http://************/center_tjbg.shtml")
#通过contains函数,提取匹配特定文本的所有元素
frame = driver.find_element_by_xpath("//iframe[contains(@src,'http://**********/cms-search/monthview.action?action=china&channelFidStr=e990411f19544e46be84333c25b63de6')]")
#进入iframe页面
driver.switch_to.frame(frame)
#获取select标签
select = driver.find_element_by_id("channelFidStr")
# 获取select里面的option标签,注意使用find_elements
options_list=select.find_elements_by_tag_name('option')
# 遍历option
for option in options_list:
#获取下拉框的value和text
print ("Value is:%s Text is:%s" %(option.get_attribute("value"),option.text))
#退出iframe
driver.switch_to_default_content()
driver.quit()

示例二:代码(BeautifulSoup遍历select选项列表):

url = "http://********************/monthview.action?action=china"
headerDict = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.31 Safari/537.36'}
data = {'riqi': '2017年12月', 'channelFidStr': 'e990411f19544e46be84333c25b63de6',
'channelIdStr': '08ce523457dd47d2aad6b41246964535'}
# psot 传递参数
res = requests.post(url, data=data, headers=headerDict)
# 获取跳转后的页面源码
soup = BeautifulSoup(res.content, "html.parser")
#获取select的选项列表
option_list = soup.find(id='channelFidStr').find_all('option')
#遍历select的选项列表
for option in option_list:
print("value:%s text:%s"%(option['value'],option.text))

作者:整合侠
链接:http://www.cnblogs.com/lizm166/p/8367615.html
来源:博客园
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Python3.x:遍历select下拉框获取value值的更多相关文章

  1. javascript遍历select下拉框判断其中值是否与指定值相等

    用jquery多了,就忘了原生的js是如何写的了,还需要多加巩固. 需求:jsp回显一select下拉框.选中指定值. 用户点击修改 该select进行已有值回显.有两种解决方法 一.js中获取用户的 ...

  2. Python3 Selenium自动化-select下拉框

    Python3 Selenium自动化-select下拉框 selenium介绍select下拉框相关的操作方法:

  3. vue select下拉框绑定默认值

    vue select下拉框绑定默认值: 首先option要加value值,以便v-model可以获取到对应选择的值 一.当没有绑定v-model,直接给对应的option加selected属性 二.当 ...

  4. 点击select下拉框获取option的属性值

    select下拉框作为前端开发者应该是经常使用的,最近在项目中遇到这样的情况,点击下拉框选项,需要获取所点击的option的属性值,当时想很简单啊,给option加一个点击事件不就行了,然后就加了一下 ...

  5. 2. select下拉框获取选中的值

    1.获取select选中的value值: $("#select1ID").find("option:selected").val();  --select1ID ...

  6. jQuery操作select下拉框的text值和value值的方法

    1.jquery获取当前选中select的text值 $("#select1").find("option:selected").text(); 2.jquer ...

  7. python+webdriver,选取Select下拉框中的值

    在选择下拉框中的值时遇到了困难,用driver.find_element_by_id("").send_keys("")进行赋值不能成功获取下拉框中的值.   ...

  8. js获取select下拉框的value值和text文本值

    介绍一种取下拉框值以及绑定下拉框数据的方法    这里用到的jquery-ui-multiselect插件 1.前台html代码 <span class="ModuleFormFiel ...

  9. js获取select下拉框中的值

    现在有一id为userType的下拉框,怎么获取选中的值: 用户类型: <select name="type" id="userType"> < ...

随机推荐

  1. 【黑金原创教程】【TimeQuest】【第七章】供源时钟与其他

    声明:本文为黑金动力社区(http://www.heijin.org)原创教程,如需转载请注明出处,谢谢! 黑金动力社区2013年原创教程连载计划: http://www.cnblogs.com/al ...

  2. LeetCode 笔记系列16.2 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]

    题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...

  3. 160304-02、JS 中如何判断null 和undefined

    JavaScript 中有两个特殊数据类型:undefined 和 null,下节介绍了 null 的判断,下面谈谈 undefined 的判断. 以下是不正确的用法: var exp = undef ...

  4. CodeForces 732D Exams

    D. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  5. PHP之冒号、endif、endwhile、endfor 是什么鬼?f

    解释:其实这些都是PHP的语法,只不过不常用而已,这些都是PHP流程控制的替代语法. 冒号(:)相当于是 左大括号---->{ endif.endwhile.endfor.endforeach- ...

  6. HOW TO FIX "EXPECTED BEGIN_ARRAY BUT WAS BEGIN_OBJECT" IN RETROFIT ?

    https://www.freshbytelabs.com/2018/05/how-to-fix-expected-beginarray-but-was.html HOW TO FIX "E ...

  7. 记录一次gitlab->github企业版的迁移

    cd到你想要存放新的工程的文件夹内, 1.使用git clone --mirror命令制作旧git的镜像 $ git clone --mirror git@git.aaaa.com:mario/my- ...

  8. windows中根据进程PID查找进程对象过程深入分析

    这里windows和Linxu系列的PID 管理方式有所不同,windows中进程的PID和句柄没有本质区别,根据句柄索引对象和根据PID或者TID查找进程或者线程的步骤也是一样的.   句柄是针对进 ...

  9. Using virtual lists

    Download demo project - 15.7 Kb Contents Introduction Virtual list Creating a virtual list Add items ...

  10. sipp模拟freepbx分机测试(SIP协议调试)

    1.sipp的安装 1) 在centos 7.2下安装 yum install make gcc gcc-c++ ncurses ncurses.x86_64 ncurses-devel ncurse ...