如何使用带有 Python 的 Selenium WebDriver 获取选定的选项?

How to get selected option using Selenium WebDriver with Python?

如何使用带有 Python 的 Selenium WebDriver 获得 selected 选项:

有人有解决方案 getFirstSelectedOption?

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

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' 的东西:

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 验证内容,例如:

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 很容易处理的事情 - 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