1

Using selenium c# - I am unable to click on a value of an angular dropdown list.

code:

new SelectElement(driver.FindElement(By.Name("Status"))).SelectByIndex(2) 

error: 'Element should have been select but is a ng-select'.

I also tried clicking on the dropdown first then targeting the values after dropdown list shows. I got 'Element should have been select but is a div'

Any Ideas on the latest way to use selenium c# for selecting values of ng select, ng options select dropdowns, would be appreciated.

HTML page

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Tobi Akin
  • 15
  • 4
  • You can not use select class for sure. Can you show us the code that you've tried for this `I also tried clicking on the dropdown first then targeting the values after dropdown list shows` – cruisepandey Aug 31 '21 at 17:15
  • driver.FindElement(By.Name("Status")).Click(); new SelectElement(driver.FindElement(By.Name("Status"))).SelectByText("Rejected"); – Tobi Akin Aug 31 '21 at 17:31

2 Answers2

0

You can try to click on drop down bar like this :

driver.FindElement(By.Name("Status")).Click();

and after that you should see all the options. Now I see you are still using select class to select the option, which will not work out since HTML is not built up using Select and options tag.

Now try to click directly :-

driver.FindElement(By.Xpath("xpath of the option should go here")).Click();

Also, I would suggest to give few seconds sleep before selecting the option.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
0

This error message...

Element should have been select but is a ng-select

...implies that the element is a element so you can't use the Select Class.


As the desired element is an angular-ngselect element so to Click() on the second option from the angular dropdown list you have to induce WebDriverWait for the ElementToBeClickable() and you can use either of the following Locator Strategies:

  • CssSelector:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("ng-dropdown-panel[aria-label='Options list']"))).Click();
    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("ng-dropdown-panel[aria-label='Options list'] div.ng-option.ng-star-inserted[id$='2']"))).Click();
    
  • XPath:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//ng-dropdown-panel[@aria-label='Options list']"))).Click();
    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//ng-dropdown-panel[@aria-label='Options list']//div[contains(@class, 'ng-option ng-star-inserted') and contains(@id, '2')]"))).Click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352