0

I want to click on an option in a drop down menu. The options have label values. The dropdown menu is not from a select element. It is an input element with a drop down arrow next to it.

The dropdown arrow has the following attributes

<span id="ctl00_ContentPlaceHolder1_ReportViewer1_ctl09_ctl21_ctl01" class="glyphui glyphui-downarrow" style="cursor: pointer;"></span>

I managed to open the dropdown menu by clicking on the dropdown arrows by doing

Bedrijfsindeling_dropdown = driver.find_element_by_xpath('//span[@id="ctl00_ContentPlaceHolder1_ReportViewer1_ctl09_ctl21_ctl01"]')
Bedrijfsindeling_dropdown.click()
time.sleep(1)

I am not able to find a way to select any option. The label looks like:

    <label for="ctl00_ContentPlaceHolder1_ReportViewer1_ctl09_ctl21_divDropDown_ctl04">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Baggerbedrijf</label>

I want to be able to select for the "Baggerbedrijf" part.

Ultimately I want to select all options one by one, but for now it is sufficient to only be able to select "Baggerbedrijf"

I tried finding the label with driver.find_element_by_xpath('//label[@for="ctl00_ContentPlaceHolder1_ReportViewer1_ctl09_ctl21_divDropDown_ctl04"")'] and then clicking on it.

However, i get the "no such element" message.

How would i be able to select the option for "Baggerbedrijf"?

  • [Can this help you out?](https://stackoverflow.com/questions/7867537/how-to-select-a-drop-down-menu-value-with-selenium-using-python) – JenilDave Jun 25 '20 at 15:17
  • unfortunately no, Sorry i should have been more clear. The dropdown menu is not from a select element – Raymond van zonneveld Jun 25 '20 at 15:20
  • @JenilDave i edited my question, hopefully it's now more clear. – Raymond van zonneveld Jun 25 '20 at 15:22
  • Is it possible to give the link of the page where your dropdown is? – JenilDave Jun 25 '20 at 15:25
  • https://www.arbeidsmarktcijfers.nl/Report/4 the dropdown i want to select in is "bedrijfsindeling" (for now. in the end i want to be able to select different dropdowns as well, but since they all behave the same (if i'm not mistaken) getting the "bedrijfsindeling" should be more then sufficient) – Raymond van zonneveld Jun 25 '20 at 15:26
  • There's given id of what you want to select. I guess Baggerbedrij. The id I got is ```ctl00_ContentPlaceHolder1_ReportViewer1_ctl09_ctl21_divDropDown_ctl04```, did you try accessing using ```find_element_by_id``` ? – JenilDave Jun 25 '20 at 15:34
  • That seems to do the trick. Thanks!. However, it would be very optimal if i could use the "Baggerbedrijf" label as this is easier to loop through. Any ideas? – Raymond van zonneveld Jun 25 '20 at 15:37

1 Answers1

1

You need to wait for the element ("Baggerbedrijf") to be visible after you click the dropdown, then identify and click it. Otherwise Selenium will try and run off thorugh the script doing its thing, without waiting for the page DOM to (re)load :). So after you click the dropdown list, add this line.

WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID,'ctl00_ContentPlaceHolder1_ReportViewer1_ctl09_ctl21_divDropDown_ctl04'))).click()

WebDriverWait requires these imports:

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

It's good practice in general to use WebDriverWait. Consider using it to identify "Bedrijfsindeling_dropdown" as well, or any other webelement for that matter.

0buz
  • 3,443
  • 2
  • 8
  • 29
  • thanks for the answer! However, JenilDave already gave me an option in the comments that made it work. But Do you perhaps know of an easy way to loop through all the options? That would be the final part of my problem. – Raymond van zonneveld Jun 25 '20 at 15:43
  • Use a straight forward `.find_element_by_id` with caution. It may work when you execute one line at a time, but unless you allow for sufficient time between actions, the risk of failure exists when running the script as a whole. – 0buz Jun 25 '20 at 15:56
  • An idea for looping through all options is to identify the list (parent element). Find all relevant child elements (something as simple as .find_elements_by_tag_name('label') may work). You may run into other issues like `StaleElementReferenceException` along the way, but that's the fun of learning, and off topic for this question :) – 0buz Jun 25 '20 at 15:59
  • I implemented time.sleep(x), however it is indeed hard to know whether I used enough sleep time. Might have to give WabDriverWait a shot. And I will look into the parent element and the child elements! Thanks! – Raymond van zonneveld Jun 26 '20 at 09:21