0

I try to choose a option in the following menu:

<select class="form-control" id="DayBirthDate" name="DayBirthDate"> . 
<option value="">--</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>


day1 = 5
day = driver.find_element_by_id('DayBirthDate')
for option in day.find_elements_by_tag_name('option'):
    if option.text == day1:
        option.click()

but can't seem to find a solution for this

2 Answers2

0

Your HTML having select for dropdown so use Selenium Select class itself instead of using standard script

Example:

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

# select by visible text
select.select_by_visible_text('1')

# select by value 
select.select_by_value('1')

# select by index
select.select_by_index(1)

Use any one , it will work for you

Source:

How to select a drop-down menu option value with Selenium (Python)

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
0

The above solution is Not Working beacuse You have declare the value day1 =5 which is integer value and when return the value option.text it is string value so code can't compare those values.

Change to day1 ='5'

day1 ='5'
day = driver.find_element_by_id('DayBirthDate')
for option in day.find_elements_by_tag_name('option'):        
    if option.text == day1:
        option.click()
KunduK
  • 32,888
  • 5
  • 17
  • 41