使用 Selenium 和 Python 列出选择选项值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18515692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:56:03  来源:igfitidea点击:

Listing select option values with Selenium and Python

pythonselenium

提问by Leonardo Cardoso

I have the following HTML code

我有以下 HTML 代码

<select name="countries" class_id="countries">
    <option value="-1">--SELECT COUNTRY--</option>
    <option value="459">New Zealand</option>
    <option value="100">USA</option>
    <option value="300">UK</option>
</select>

I am trying to get a list of the option values (like 459, 100, etc, not the text) using Selenium.

我正在尝试使用 Selenium 获取选项值列表(如 459、100 等,而不是文本)。

At the moment I have the following Python code

目前我有以下 Python 代码

from selenium import webdriver

def country_values(website_url):
    browser = webdriver.Firefox()
    browser.get(website_url)
    html_code=browser.find_elements_by_xpath("//select[@name='countries']")[0].get_attribute("innerHTML")
    return html_code

As you can see the code returns pure HTML, which I am parsing with HTMLParser library. Is there any way to get the option values just using Selenium? In other words, without having to parse the result from Selenium?

如您所见,代码返回纯 HTML,我正在使用 HTMLParser 库对其进行解析。有没有办法只使用 Selenium 来获取选项值?换句话说,无需解析来自 Selenium 的结果?

采纳答案by TehTris

check it out, here is how i did it before i knew what the Select Module did

检查一下,这是我在知道选择模块做了什么之前我是怎么做的

from selenium import webdriver

browser = webdriver.Firefox()
#code to get you to the page

select_box = browser.find_element_by_name("countries") 
# if your select_box has a name.. why use xpath?..... 
# this step could use either xpath or name, but name is sooo much easier.

options = [x for x in select_box.find_elements_by_tag_name("option")]
# this part is cool, because it searches the elements contained inside of select_box 
# and then adds them to the list options if they have the tag name "options"

for element in options:
    print element.get_attribute("value") 
    # or append to list or whatever you want here

outputs like this

像这样的输出

-1
459
100
300

回答by unutbu

import selenium.webdriver as webdriver
import selenium.webdriver.support.ui as UI
import contextlib

with contextlib.closing(webdriver.Firefox()) as driver:
    driver.get(url)
    select = UI.Select(driver.find_element_by_xpath('//select[@name="countries"]'))
    for option in select.options:
        print(option.text, option.get_attribute('value'))  

prints

印刷

(u'--SELECT COUNTRY--', u'-1')
(u'New Zealand', u'459')
(u'USA', u'100')
(u'UK', u'300')

I learned this here. See also the docs.

我在这里学到了这个。另请参阅文档

回答by Teodor Ciuraru

Simpler version:

更简单的版本:

dropdown_menu = Select(driver.find_element_by_name(<NAME>))
for option in dropdown_menu.options:
        print option.text