0

I have a dropdown menu with 7 values and these 7 values are stored in the form of list items under unordered list as:

<ul class="rcbList" style="list-style:none;margin:0;padding:0;zoom:1;">
   <li class="rcbItem ">--Select--</li>
   <li class="rcbHovered ">PIPL-C1-BH-RILJM</li>
   <li class="rcbItem ">PIPL-C1-BH1-RILJM</li>
   <li class="rcbItem ">PIPL-C1-BH2-RILJM</li>
   <li class="rcbItem ">PIPL-C1-BH-RPLJM</li>
   <li class="rcbItem ">PIPL-C1-DHJ-RPLJM</li>
   <li class="rcbItem ">PIPL-C1-DHJ-RILJM</li>
</ul>    

I want to click on each value of this dropdown menu using for loop with the help of selenium chromedriver in python.
Suppose I want to click on 2nd value PIPL-C1-BH-RILJM, I can do it as :

driver.find_element(By.XPATH,"//div[@id='ContentPlaceHolder1_rcmbCapacityTranch_DropDown']/div/ul/li[2]").click()

But to use it in the for loop, I have to do indexing on li tag. So, when I write as:

i=2
driver.find_element(By.XPATH,"//div[@id='ContentPlaceHolder1_rcmbCapacityTranch_DropDown']/div/ul/li['+str(i)+']").click()

It shows only --Select-- and does not select the 2nd value and same happens with other values of i.
I have also tried as:

i=2
driver.find_element(By.XPATH,"//div[@id='ContentPlaceHolder1_rcmbCapacityTranch_DropDown']/div/ul/li['"+str(i)+"']").click()

But --Select-- is selected again from the dropdown menu.
So, can anyone please help me to click on a particular value with indexing on li tag.
Any help would be appreciated.

1 Answer 1

1

Try like below once:

Collect all the li tag elements in a list and then iterate over them. Use find_elements for the same.

# This should highlight all the li tags - //div[@id='ContentPlaceHolder1_rcmbCapacityTranch_DropDown']/div/ul/li
options = driver.find_elements(By.XPATH,"//div[@id='ContentPlaceHolder1_rcmbCapacityTranch_DropDown']/div/ul/li")

# This should print 7
print(len(options))

# Start the loop from index 1 since the first option is "--Select--"
for i in range(1,len(options)):
    options[i].click()
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @pmadhu.

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.