如何使用带有Python的Selenium WebDriver获得选择的选项? [英] How to get selected option using Selenium WebDriver with Python?

查看:102
本文介绍了如何使用带有Python的Selenium WebDriver获得选择的选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过Selenium WebDriver和Python获取选择的选项:

How to get selected option using Selenium WebDriver with Python:

有人对getFirstSelectedOption有解决方案吗?

我正在使用它来获取选择元素:

I'm using this to get the select element:

try:
    FCSelect = driver.find_element_by_id('FCenter')
    self.TestEventLog = self.TestEventLog + "<br>Verify Form Elements: F Center Select found"
except NoSuchElementException:
    self.TestEventLog = self.TestEventLog + "<br>Error: Select FCenter element not found"

是否存在与'getFirstSelectedOption'类似的东西或类似的东西:

Is there an equivalent or something close to 'getFirstSelectedOption' like this:

try:
    FCenterSelectedOption = FCenterSelect.getFirstSelectedOption()
    self.TestEventLog = self.TestEventLog + "<br>Verify Form Elements: F Center Selected (First) found"
except NoSuchElementException:
    self.TestEventLog = self.TestEventLog + "<br>Error: Selected Option element not found"

然后,我想使用getText来验证内容:

Then I would like to Verify the Contents with a getText like:

try:
    FCenterSelectedOptionText = FCenterSelectedOption.getText()
    self.TestEventLog = self.TestEventLog + "<br>Verify Form Elements: FCenter Selected Option Text found"
except NoSuchElementException:
    self.TestEventLog = self.TestEventLog + "<br>Error: Selected Option Text element not found"

if FCenterSelectedOptionText == 'F Center Option Text Here':
    self.TestEventLog = self.TestEventLog + "<br>Verify Form Elements: F Center Selected Option Text found"
else:
    self.TestEventLog = self.TestEventLog + "<br>Error: F Center 'Selected' Option Text not found"

推荐答案

这是selenium使其易于处理的功能-

This is something that selenium makes it easy to deal with - the Select class:

from selenium.webdriver.support.select import Select

select = Select(driver.find_element_by_id('FCenter'))
selected_option = select.first_selected_option
print selected_option.text

这篇关于如何使用带有Python的Selenium WebDriver获得选择的选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆