1

I have below html code, all I want to click on the dropdown and select the first value.How can I achieve this.I am having issues when selecting the values from dropdown but was able to click the dropdown

<div id= "location-select-list" class="mb-list" role="role0">
    <mb-option id='1' class='classname' role='rolename' data-auto-id="dt1" aria-disabled="False" 1 </mb-option>
    <mb-option id='2' class='classname' role='rolename' data-auto-id="dt2"aria-disabled="False" 2 </mb-option>

I have tried this but not working.

#click on the dropdown--working

WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH,
                                        xpath_0))).click()
#selecting 1st value from the dropdown value list--not working

xpath = "//div[@id='location-select-list']//mb-option[@data-auto-id='dt1']"
WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH,
                                        xpath))).click()
14
  • 1) Do //div[@id='location-select-list']//mb-option[@data-auto-id='dt1'] is the unique locator 2) Have you tried to use visibility_of_element_located instead of element_to_be_clickable especially for the second command? 3)Can you share a link to that page? Commented Dec 7, 2021 at 19:27
  • Tried with visibility_of_element_located using select. but it's throwing error since the the dropdown is coded inside div.And yes the xpath is unique Commented Dec 7, 2021 at 19:38
  • I see no select n your HTML. So, if it is not there, why to use it? Commented Dec 7, 2021 at 19:39
  • The code that I provided doesn't include any select Commented Dec 7, 2021 at 19:41
  • But in the previously comment you mentioned "using select".. Commented Dec 7, 2021 at 19:42

1 Answer 1

2
<div id= "location-select-list" class="mb-list" role="role0">
    <mb-option id='1' class='classname' role='rolename' data-auto-id="dt1" aria-disabled="False" 1 </mb-option>
    <mb-option id='2' class='classname' role='rolename' data-auto-id="dt2"aria-disabled="False" 2 </mb-option>

Try this

#click on the dropdown--working
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH,
                                        xpath_0))).click()

#my code
elem_list = driver.find_elements_by_class_name('classname') 
elem_list[0].click()

Or

elem_list = driver.find_elements(By.XPATH, "//mb-option[contains(@class, 'classname')]"):
elem[0].click()
Sign up to request clarification or add additional context in comments.

2 Comments

It worked , thanks a lot. But there is one more issue. I have one more dropdown with the same class name like this.<div id= "timezone-select-list" class="mb-list" role="role0"> <mb-option id='3' class='classname' role='rolename' data-auto-id="dt3" aria-disabled="False" 1 </mb-option> <mb-option id='4' class='classname' role='rolename' data-auto-id="dt4"aria-disabled="False" 2 </mb-option> Is there any way to refer to the top level id which is here timezone-select-list
You can use the same strategy creating a list of elements with the same classname and clicking on then as you like. list_of_select = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, //div[contains(@class, 'mb-list')] ))) list_of_select[0].click() #then do whatever you have to list_of_select[1].click() #then do whatever you have to (If you can't figure it out, please, create a new question and a take a looke as soon as possible)

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.