Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to select option from a combobox in python selenium?

I am trying to put value in a combo-box using python selenium. As we enter text it gives different possible values in dropdown. How to select the first value on the drop down? As soon as I try to click on it, it removes the text and makes it blank.

enter image description here
enter image description here

So, for example, I am sending the text Stockholms kommun, Stockholms län in the combobox by:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

text = Stockholms kommun, Stockholms län
inputcity = driver.find_element(By.XPATH, '//*[@id="price-calculator-address"]')
inputcity.send_keys(text)

However, I am not able to click (as shown in image-2) on the dropdown which appears as I enter the text.

Link of website: https://www.hemnet.se/annonsera-bostad#priskalkylatorn

Can anyone help me with it?

>Solution :

Trick is to enter the text and press Tab twice. Basically imitate the manual actions if it was to be done via human.

Try the below working code:

driver = webdriver.Chrome()
driver.get("https://www.hemnet.se/annonsera-bostad#priskalkylatorn")
driver.maximize_window()
# below line will accept cookies
driver.find_element(By.XPATH, "//button[text()='Jag samtycker']").click()
driver.find_element(By.XPATH, "//*[@id='price-calculator-address']").send_keys("Stockholms kommun, Stockholms län")
time.sleep(3)
# below line will press TAB key 2 times
driver.find_element(By.XPATH, "//*[@id='price-calculator-address']").send_keys(Keys.TAB + Keys.TAB)

Result:
enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading