1

How to select an option from drop down options.

Here is the code of drop down in html.

<select name="birthDate[month]" 
class="monthSelect">
<option value="">Month</option>
<option value="1">january</option>
<option value="2">feburary</option>

For other item selection I am using ID to find the elements. In this case there is no ID associated to the options.

How to select the options from this drop down based on text or sequence number or by value.

Gaurav
  • 3,615
  • 2
  • 27
  • 50
witt32
  • 41
  • 4
  • Make question more easy to read, So other who will read the question will get good idea about what is asked. – Gaurav Nov 20 '19 at 05:48

2 Answers2

0

You can use Select:

element = Select(driver.find_element_by_name('birthDate[month]'))

#by value
element.select_by_value('2')

#by visible text
element.select_by_visible_text('feburary')

#by index
element.select_by_index(2)

Following import:

from selenium.webdriver.support.ui import Select

Or if you want use xpath:

driver.find_element_by_xpath('//select[@name="birthDate[month]"]//option[@value="2"]').click()
frianH
  • 7,295
  • 6
  • 20
  • 45
0

Here is the sample code to select the drop down:-

You should use the below imports

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

Webdriver d = webdriver.Firefox()
d.get('Web site url')

sel = Select(driver.find_element_by_id('element_path'))

# To select by visible text use this
sel.select_by_visible_text('Banana')

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

# To select by index
sel.select_by_index('1')
404pio
  • 1,080
  • 1
  • 12
  • 32