-1

When trying to read a specific value from dropdown which is in span. It throws error "element should have been select but was span"

I am tried reading the element, click the element and then try reading the text but nothing works

protected By entityIDdrpdwn => By.XPath("//span[@class='k-input']");
string value = "Campaign";            

ClickElement(entityIDdrpdwn);                   
SelectValueFromDropdown(entityIDdrpdwn,value);

enter image description here

I expect the the element from dropdown "Campaign" should be selected but it throws error

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
Analyst
  • 105
  • 1
  • 9

1 Answers1

0

The method SelectValueFromDropdown(entityIDdrpdwn,value) presumably uses org.openqa.selenium.support.ui.Select class which deals with elements that are present inside the select html tag. Elements belonging to the select tag contain attributes like name , value etc. and the Select class interacts with them. This is the reason you are getting an error element should have been select but was span.

You can try to get the span element and then get the underlying value.

Also, you can refer to https://www.guru99.com/select-option-dropdown-selenium-webdriver.html to understand more about the Select class as this webpage does have good visualizations.

work_ishaan
  • 354
  • 1
  • 8
  • I tried to get the span. The problem is that the value within span changed based on the selecting which is like : ::before "Value". So when trying to find and select that specific value using span which contains text, it throws an error- Could not find element. for eg: FindElement(By.XPath("//span[@class='k-input'][contains(text(),'AD'")]); – Analyst Aug 15 '19 at 14:34