-1

I have three drop-downs with same ID's and want to click each of them, one by one, but it will click the same element three times. The remaining two elements are failing, but that test case is passed.

How can I handle this scenario?

  • 3
    As an aside: Your developers should fix the site so it does not have multiple elements with the same ID. This is not valid HTML. – anonygoose Mar 29 at 11:51
  • Is it possible for you to share the HTML code in here. That can be helpful for people looking at your question and also for you. Cheers! – K.K Mar 29 at 11:53
  • Try accessing the element using xpath instead of id. – Shreyanka Manekar Mar 30 at 7:12
1

There are multiple ways, it depends on you which one you want to use:

  1. If dropdown actions are one after another:

    List<WebElement> element= driver.findElements(By.id("xyz"));
    for(WebElement value : element) { value.click(); }
    
  2. If performing some actions between them:

    driver.findElement(By.xpath(".//select[@id='xyz'][1]")).click();  
    

    Here [1] is the index value of dropdown, so you can use your index value.

  3. You can use the CSS value because maybe that's different.

If you want to select the option values of dropdown then replace the click event by select events like selectByVisibleText, etc.

0

Not sure what language you are using as base. Assuming you want to interact with them at certain points in a "static" manner you can use findElements instead of findElement

findElement will find the first instance of the element you search even if there are more present

findElements will return you a list of elements, after that you can select the one you want to select.

Share the code of the dropdown there might be a way to do this instead of using findElements

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.