How to get selected option using Selenium WebDriver with Python:
Someone have a solution for a 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"
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"
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"
This is something that
selenium
makes it easy to deal with - theSelect
class: