How to interact with web elements with the same ID's?



  • 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?



  • 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.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2