2

I have a dropdown and lets say the dropdown's code looks something like this:

select name="Rv$ct104$ct1111Value" onchange="javascript:setTimeout('___do PostBack(\'Rv$ct104$ct1111Value')',0)" id="RV_ct104_ct111_ddValue" class="aspNetDisabled" style="font-family": Verdana; font-size:8pt; width:281px;"
<option selected="selected" value="1">(ALL)</option>
<option value="2"Bob&nbsp;LastName1</option>
<option value="3"George&nbsp;LastName2</option>
<option value="4"Alice&nbsp;LastName3</option>
<option value="5"Michael&nbsp;LastName4</option>
<option value="6"Mary&nbsp;LastName5</option>

How will I select 'Alice' for my web-scraper with selenium and python if the option value for Alice is constantly changing? New names are constantly being added to the dropdown therefore the option value is constantly changing, but I need my web scraper to select same name always.

Michael
  • 63
  • 9

1 Answers1

0

select_by_visible_text is probably what you need. Also add waits for dropdown to load when you open it

from selenium.webdriver.support.select import Select
text_dropdown = "Alice"

dropdown_locator = driver.find_element_by_id(element)
dropdown_locator.click()
select = Select("dropdown_locator")
if locator is not None:
    for option in select.options:
        select.select_by_visible_text(text_dropdown)

Answers to this question contain more options:

How to select a drop-down menu value with Selenium using Python?

vitaliis
  • 4,082
  • 5
  • 18
  • 40