Select an option from the Selenium Python drop down menu

Asked

Viewed 2,076 times

1

I have a menu that presents several options, I want to select only the one that is active. When I give 'variable'. find_element_by_id('key') Selenium returns me ALL options. The active option has a "Selected" marker, as I point to is marked in Selenium?

variable = nanana.find_element_by_id('key')

<tr>
            <td class="label"><label for="key"><em>*</em>Key:</label></td>
            <td>
                <select name="key" id="key"><option value="72-N-PR-2">ADEMAR&nbsp;</option>
                    
                        <option value="32-N-PR-2" selected="selected">TEIXEIRA&nbsp;</option>
                    
                        <option value="14-N-PR-2">ANA&nbsp;</option>
                    
                        <option value="45-N-PR-2">RITA&nbsp;</option>
                    
                        <option value="47-N-PR-2">ANDRE&nbsp;</option>                      
                        
                        <option value="23-N-PR-2">JEANE&nbsp;</option>
                    
                        <option value="45-N-PR-2">JOAO&nbsp;</option>
                    
                        <option value="30-N-PR-2">LUIZ&nbsp;</option>
                                            
                        <option value="15-N-PR-2">VALDYR&nbsp;</option>
                    
                        <option value="4-N-AM-2">VANESSA&nbsp;</option>
                    
                        <option value="05-N-PR-2">GARCIA&nbsp;</option></select>
            </td>                   
        </tr>
  • What do you mean by "active option"? vc executed a click? want to select a specific element?

  • When I say active I mean "selected". Of all the options in the menu, it is the one that is currently visible. I do not intend to click, but capture the text for further processing, because the active text is the one that selects the active screen. If I select another option from the menu, it takes me to another screen.

  • Vc is using a 'from Cratch" script or is generating through the Selenium IDE?

  • I’m using Selenium with Python, trying to avoid the use of Beautifulsoup (not to depend on another library). Selenium has located everything I need so far... by testing in Python Idle itself.

  • Okay, put a possible answer, see if it helps.

  • What? the answer answered? :-)

  • Thanks friend! The only change I made was to put "select.first_selected_option"! I am very grateful!

Show 2 more comments

1 answer

4


Seleneium has a class to work with select->option, try this:

from selenium import webdriver
from selenium.webdriver.support.ui import Select

driver = webdriver.Firefox()
driver.get('url')

select = Select(driver.find_element_by_id('key'))

# selecionando pelo texto visivel
select.select_by_visible_text('TEIXEIRA')

# Selecionando pelo valor
select.select_by_value('30632-N-PR-2')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.