S Andrew
S Andrew

Reputation: 7208

How to select a value from drop down menu in python selenium

I am writing a python script which will call a webpage and will select an option from the drop down to download that file. To do this task, I am using chropath. It is a browser extension which can give you the relative xpath or id for any button or field on the webpage and using that we can call it from python selenium script.

enter image description here

Above image shows the drop down menu in which I have to select 2019 as year and the download the file. In the lower part of the image, you can see that I have used chropath to get the relative xpath of the drop down menu which is //select[@id='rain']

Below is the code I am using:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get("<URL>")

driver.maximize_window()

grbf = driver.find_element_by_xpath("//select[@id='rain']") 
grbf.send_keys('2019')

grbf_btn = (By.XPATH, "//form[1]//input[1]")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(grbf_btn)).click()

from the above code, you can see that I am using xpath to select the drop down grbf = driver.find_element_by_xpath("//select[@id='rain']") and then sending keys as 2019 i.e. grbf.send_keys('2019') and after that I am calling download button to download it. But for some reason, its always selecting year 1999 from the drop down. I am not able to understand what is wrong in this. Is this correct approach to solve this. Please help. Thanks

Upvotes: 0

Views: 16971

Answers (3)

Mr.Kim
Mr.Kim

Reputation: 176

In my opinion, I don't think this is the correct approach. You try to select the option which is dropdown (not a text box like ), so send key command does not work.

What you need to do is try to inspect HTML changing when clicking the dropdown and try to XPath for an option that you want to select.

If you still stuck at this problem, I recommend using katalon recorder which is a chrome extension to allow you to record and do UI testing

Upvotes: 1

SeleniumUser
SeleniumUser

Reputation: 4177

Try below code:

select = Select(driver.find_element_by_xpath("//select[@id='rain']"))
select.select_by_visible_text('2019')

Another approches to deal with dropdown:

Using Index of Dropdown :

select.select_by_index(Paass index)

Using valueof Dropdown :

select.select_by_value('value of element')

Using visible text of Dropdown :

select.select_by_visible_text('element_text')

Upvotes: 2

Rub&#233;n Paredes
Rub&#233;n Paredes

Reputation: 114

I had the same problem time ago. Try this:

from selenium.webdriver.support.ui import Select

grbf = Select(driver.find_element_by_xpath("//select[@id='rain']"))
grbf.select_by_value('2019')

In the select_by_value() you have to use the value of the element in the dropdown.

By the way, if an element has id, use it.

grbf = Select(driver.find_element_by_id('rain'))

Upvotes: 5

Related Questions