Select an option from the drop down menu Selenium Python

0

I have a menu that has 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. Does the active option have a "selected" marker, as I point to this selenium markup?

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="29972-N-PR-2">ADEMAR&nbsp;</option>

                        <option value="30632-N-PR-2" selected="selected">TEIXEIRA&nbsp;</option>

                        <option value="40914-N-PR-2">ANA&nbsp;</option>

                        <option value="16145-N-PR-2">RITA&nbsp;</option>

                        <option value="21147-N-PR-2">ANDRE&nbsp;</option>                       

                        <option value="52323-N-PR-2">JEANE&nbsp;</option>

                        <option value="18645-N-PR-2">JOAO&nbsp;</option>

                        <option value="22930-N-PR-2">LUIZ&nbsp;</option>

                        <option value="14015-N-PR-2">VALDYR&nbsp;</option>

                        <option value="4354-N-AM-2">VANESSA&nbsp;</option>

                        <option value="23305-N-PR-2">GARCIA&nbsp;</option></select>
            </td>                   
        </tr>
    
asked by anonymous 13.07.2017 / 19:52

1 answer

1

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')
    
13.07.2017 / 22:03