2015-06-16 26 views
8

Jak uzyskać wybraną opcję za pomocą Selenium WebDriver z pytharem:Jak uzyskać wybraną opcję za pomocą Selenium WebDriver z Pythonem?

Ktoś ma rozwiązanie dla getFirstSelectedOption?

używam tego, aby uzyskać wybrać 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" 

Czy istnieje odpowiednik lub coś blisko „getFirstSelectedOption” jak ten:

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" 

Następnie chciałbym Sprawdź, czy Zawartość z getText podoba się:

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" 

Odpowiedz

18

To jest coś, co selenium ułatwia obsługę - klasa Select:

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