Skip to content
Advertisement

Selenium how to select first option if page has multiple select box using python

I have 3 select box’s in my page

example :

<select>
  <option value="1">Volvo</option>
  <option value="2">Saab</option>
</select> 

<select>
  <option value="cat">Cat</option>
  <option value="dog">Dog</option>
</select> 

<select>
  <option value="1">Banana</option>
  <option value="2">Mango</option>
</select> 

If there has 5 or 6 select box I will always select 1st option, I am unable to do it using selenium. I have tried below code

selectboxs = driver.find_element_by_xpath("//select/option[@value=1]")

for selectbox in selectboxs:
    if selectbox:
    selectbox.click()

But no any impact, How can I solve this problem ?

Advertisement

Answer

You are very near of the solution !

selectboxs = driver.find_elements_by_xpath("//select/option[1]")

See the documentation carefully how they select First input child element of the form element with attribute id set to loginForm.

note : It’s elements not element !

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement