1

I've been trying without success to select an option from a dropdown select 2 class.

<span class="select2-selection__rendered" id="select2-OperativeUnit_Id-container" title="Newsan">Newsan</span>

I did this then in Selenium and the dropdown is selected:

driver.findElement(By.xpath("//*[@id=\"select2-OperativeUnit_Id-container\"]")).click();

The problem is that i can select no option at all. The dropdown has this:

<ul class="select2-results__options" role="tree" id="select2-OperativeUnit_Id-results" aria-expanded="true" aria-hidden="false">
<li class="select2-results__option select2-results__option--highlighted" id="select2-OperativeUnit_Id-result-1jq8-81" role="treeitem" aria-selected="true">Company1</li>
<li class="select2-results__option" id="select2-OperativeUnit_Id-result-fjep-281" role="treeitem" aria-selected="false">Company2</li>
<li class="select2-results__option" id="select2-OperativeUnit_Id-result-e8a1-408" role="treeitem" aria-selected="false">Company3</li>
</ul>

What should i do to select the option "Company2"?

3
  • Can you post the URL?
    – AbiSaran
    Commented Oct 4, 2022 at 14:18
  • Hey pal. it's not public at all, it's hidden
    – JustToKnow
    Commented Oct 4, 2022 at 14:19
  • @AbiSaran i don't understand how to select an option from a select2 dropdown, tried a lot of different things
    – JustToKnow
    Commented Oct 4, 2022 at 14:20

2 Answers 2

1

Instead of

driver.findElement(By.xpath("//*[@id=\"select2-OperativeUnit_Id-container\"]")).click();

Try

driver.findElement(By.xpath("//li[contains(.,'Company2')]")).click();

You can make the locator more precise with this:

driver.findElement(By.xpath("//li[@class='select2-results__option' and contains(.,'Company2')]")).click();
12
  • 1
    Hey pal, i'm checking it out. It seems to be selecting it but the option selected vanish
    – JustToKnow
    Commented Oct 4, 2022 at 14:27
  • You didn't (can't) share the link and all your code so I'm not sure we can ques why the option get disappearing
    – Prophet
    Commented Oct 4, 2022 at 14:31
  • No, it is not getting selected it is like "refreshing" the page
    – JustToKnow
    Commented Oct 4, 2022 at 14:37
  • 1
    The second locator contains class attribute making the locator more unique and stable
    – Prophet
    Commented Oct 4, 2022 at 15:10
  • 1
    It worked by doing this: driver.findElement(By.xpath("//*[@id=\"AccountNumber_1\"]")).sendKeys("001712");
    – JustToKnow
    Commented Oct 4, 2022 at 15:21
1

Try:

driver.findElement(By.xpath("//*[@id=\"select2-OperativeUnit_Id-container\"]/li[2]").click()

#OR

driver.findElement(By.xpath("//li[contains(text(),'Company2')]")).click()
3
  • Hey, thnks for replying!. No, it is not working. It is not getting selected.
    – JustToKnow
    Commented Oct 4, 2022 at 14:23
  • No, i don't know why but it is not working at all. The XPATH from option Company 2 is: //*[@id="select2-OperativeUnit_Id-result-spkb-408"]
    – JustToKnow
    Commented Oct 4, 2022 at 14:43
  • Hey pal, i solved this by using this: driver.findElement(By.xpath("//li[@class='select2-results__option' and contains(.,'Company2')]")).click(); from Prophet. Why his method worked but different methods dont? why?
    – JustToKnow
    Commented Oct 4, 2022 at 14:57

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.