-3

How do I select a value from drop down in selenium webdriver using python?

I am unable to access the drop down elements.elements are in form of labels.

Expected is i want to pick a value from drop down. and value don't have any id. when i try to right click on my elements in drop down,drop down gets closed.

Screen shot when drop down is opened:

Screen shot when drop down is opened

Image when down is closed:

Image when down is closed

Find the code to click the drop down

lead_quality_dropdownclick = driver.find_element_by_xpath('//*[@id="vs"]/div[1]/div/div/div/div[2]/div/div[1]/main/div/div[3]/div/div/div[1]/div/div[1]/div/div/div[2]/div')
lead_quality_dropdownclick.click()
print("Lead quality drop down is opened")
3
  • 3
    Can you share the code you have tried so far? Commented Feb 4, 2020 at 14:16
  • Please read why a screenshot of code is a bad idea. Paste the code and properly format it instead. Commented Feb 4, 2020 at 16:09
  • Xpath you are using is absolute and fragile, you should go for relative xpath. Commented Feb 5, 2020 at 12:28

1 Answer 1

0

You can ask Selenium to click on the arrow behind the drop down menu and than let it search for your content.

To click, you can use:

      from selenium.webdriver.support.ui import WebDriverWait  # Allows to wait for a page to load
      from selenium.webdriver.support import expected_conditions as EC  # Specify what you are looking for on a specific page

      time_out = 20 #wait till the site is loaded before you click
      try:
         WebDriverWait(driver,
              time_out).until(EC.element_to_be_clickable((By.XPATH, 'put the path here'))).click() 

The XPath can be found by a right click on the code that leads to the arrow and copy the XPath.

Sign up to request clarification or add additional context in comments.

5 Comments

hi sir.Thank you for your reply. While the issue is am not able to right click the drop down. If i click them drop down vanishes or closes. I wrote code till clicking the drop down.Find the code below: lead_quality_dropdownclick = driver.find_element_by_xpath('//*[@id="vs"]/div[1]/div/div/div/div[2]/div/div[1]/main/div/div[3]/div/div/div[1]/div/div[1]/div/div/div[2]/div') lead_quality_dropdownclick.click() print("Lead quality drop down is opened")
Does it print; ‘lead quality drop down is opened?’ Otherwise it might be that you have to enter a time-out (to wait) or a different path. If this does not work, can you update your question with the complete code and a link to the site? Otherwise I’m not able to test this and help you in finding the answer.
Have you tried using the arrow down key? You could press arrow down key a number of times till you get to the item, after clicking on the arrow button. Also, what I do when all things fail is to write a javascript code that does what I want, and then run it with driver.execute_script()
Hi Yes print is working for me. but i don't know how to pick value from drop down as its too dynamic.
check this site for the drop down list values. or this StackOverFlow question. or follow this site's instructions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.