0

I have this dropdown:

<select class="admin__control-select" data-bind="
    attr: {
        name: inputName,
        id: uid,
        disabled: disabled,
        'aria-describedby': noticeId
    },
    hasFocus: focused,
    optgroup: options,
    value: value,
    optionsCaption: caption,
    optionsValue: 'value',
    optionsText: 'label'" name="product[business_line]" id="N2JWY3F" aria-describedby="notice-N2JWY3F"><option value=""> </option><option data-title="PU - 21" value="425">PU - 21</option><option data-title="PU - 35" value="430">PU - 35</option></select>

The XPath is:

//*[@id="N2JWY3F"]

It has 2 options available: PU - 21 and PU - 35. I want to select the option: PU - 21.

I did this:

First, click on that dropdown:

driver.findElement(By.xpath("//*[@id=\"N2JWY3F\"]")).click();

How can I specify the option that I want? I tried different things and not one worked.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
JustToKnow
  • 785
  • 6
  • 23
  • 1
    Does this answer your question? [How to select a dropdown value in Selenium WebDriver using Java](https://stackoverflow.com/questions/20138761/how-to-select-a-dropdown-value-in-selenium-webdriver-using-java) – SiKing Oct 05 '22 at 15:18
  • @SiKing i did this but it did not worked: Select dropdown = new Select(driver.findElement(By.xpath("//*[@id=\"APMNRVR\"]"))); Thread.sleep(6000); dropdown.selectByVisibleText("PU - 21"); – JustToKnow Oct 05 '22 at 15:23
  • Can you explain "it did not work"? – SiKing Oct 05 '22 at 19:13
  • Also, you can trim your code to use just `By.id("N2JWY3F")` instead of that mess of escaped quotes. – SiKing Oct 05 '22 at 19:14

2 Answers2

0

I'm guessing the drop-down isn't being clicked properly; can you check if "/*[@id="N2JWY3F"]" has only one match, and if so, maybe you can experiment with some other attributes:

driver.findElement(By.id("N2JWY3F"));
OR
driver.findElement(By.name("product[business_line]"));
OR
driver.findElement(By.className("admin__control-select"));

If updating the locator does not work, make sure the page is loaded and the drop-down menu is enabled before clicking. You can either wait or make sure the drop-down menu is visible on the page.

Pooja Jadhav
  • 171
  • 5
0

To select a value from the dropdown on the DOM you can utilize the Select class in Selenium.

Select drpDown = new Select(driver.findElement(By.id("N2JWY3F")));
drpDown.selectByVisibleText("PU - 21");

Basically first assign the dropdown to the Select class drpDown and then use one of the available methods to get the option you want.

possum
  • 1,837
  • 3
  • 9
  • 18