How to select dropdown item with same data-testid

Im trying to get selenium to click an option in a dropdown. There are several options and it looks like the only unique thing about them is the link text. They all have 'data-testid' and they are all the same. Anyone see a way to click a specific choice within the dropdown?

Here are a few examples of options within the dropdown: (App installs and Lead Generation)

<li class="" data-testid="ads-mini-grouped-objective-selector-option" style=""><div aria-checked="false" aria-disabled="false" class="_2wpb _3v8w" data-testid="SUISelectorOption/container" role="menuitem" style="letter-spacing: normal; color: rgb(75, 79, 86); font-size: 12px; font-weight: normal; font-family: Arial, sans-serif; line-height: 16px; padding: 6px 24px 6px 32px;" tabindex="-1"><span class="_27_z _4s-j" style="margin-left: 0px; margin-right: 8px;"></span><div class="_3leq"><span>App installs</span></div></div></li>

<li class="" data-testid="ads-mini-grouped-objective-selector-option" style="background-color: rgba(29, 33, 41, 0.08);"><div aria-checked="false" aria-disabled="false" class="_2wpb _3v8w" data-testid="SUISelectorOption/container" role="menuitem" style="letter-spacing: normal; color: rgb(75, 79, 86); font-size: 12px; font-weight: normal; font-family: Arial, sans-serif; line-height: 16px; padding: 6px 24px 6px 32px;" tabindex="-1"><span class="_27_z _5da8" style="margin-left: 0px; margin-right: 8px;"></span><div class="_3leq"><span>Lead generation</span></div></div></li>

I have tried to select by link text but that did not work.

4 answers

  • answered 2018-02-13 01:35 Deepan

    driver.findElement(By.xpath("(//li[@data-testid='ads-mini-grouped-objective-selector-option'])[2]")).click();
    

    Try this xpath to click second data with same data-testid:

  • answered 2018-02-13 01:35 har07

    You can use XPath to select li element by attribute data-testid and inner text :

    //li[@data-testid='ads-mini-grouped-objective-selector-option' and .='Lead generation']
    

  • answered 2018-02-13 01:35 Vincent

    har07 answer is a good answer, you can also try this as an option

    //*[text()='App installs']/parent::div/parent::div/parent::li
    

    Because not all the html is given, this is the only option i can get

  • answered 2018-02-13 01:35 DebanjanB

    To click an option from the Dropdown you can write a function as clickOption() as follows :

    public void clickOption(String option)
    {
        driver.FindElement(By.XPath("//li[@data-testid='ads-mini-grouped-objective-selector-option']//span[.='" + option + "']")).Click();
    }
    

    Now, from your main() or @Test annotation Class, call the function clickOption() method with the option which you want to select as follows :

    clickOption("App installs");
    //or
    clickOption("Lead generation");