1

Here I am trying to select a value from dropdown using selenium script but I got this error in the console like

"Exception in thread "main" org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "span"..

public class HomeUserManagement {

public static void main(String args[]) {
    System.setProperty("webdriver.chrome.driver", 
"C:\\Users\\UMASHANKAR\\Documents\\selenuim\\chromedriver.exe");
    WebDriver driver=new ChromeDriver();
    driver.manage().window().maximize();

//for login
    driver.get("https://ecabportal.azurewebsites.net/dashboard");

driver.findElement(By.name("email")).sendKeys("[email protected]");

driver.findElement(By.name("password")).sendKeys("abc123xyz");
    driver.findElement(By.name("signIn")).click();  


//actual code for selecting a value from dropdown

 driver.get("https://ecabportal.azurewebsites.net/user");
    Select drpdwn=new Select(driver.findElement(By.id("select2-signup-username-container")));
    drpdwn.selectByVisibleText("User Name");
    drpdwn.selectByIndex(0);

there are multiple values in a dropdown I need to select one value in that..

6
  • anybody help me Commented Jun 6, 2019 at 6:24
  • Can you post the (minimal amount of) HTML to reproduce? Commented Jun 6, 2019 at 6:51
  • @orde OP has provided the URL... no need for HTML... Commented Jun 6, 2019 at 7:05
  • @MosheSlavin There IS a need for the HTML in the question. The site design may change tomorrow and this question will be useless to future readers. The relevant HTML should always be included in the question. Having said that, additionally adding a link to the page is many times helpful in case the OP's definition of "relevant" HTML isn't all that's actually needed. Commented Jun 7, 2019 at 15:43
  • Possible duplicate of org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "td" Commented Jun 7, 2019 at 15:44

2 Answers 2

3

As the error shows you are using <span> tag not Select.

The Select element you are looking for is //*[@id="signup-username"].

Also, you should use WebDriverWait to wait for your locators:

WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));

You should look at ExpectedConditions to wait for...

Hope this helps you!

Sign up to request clarification or add additional context in comments.

2 Comments

if i use this code..i will get exception like this "Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: element not interactable"
this html code they were using for dropdown <span class="select2-selection__rendered" id="select2-signup-username-container" title="Select">Select</span>
0

This error message...

"Exception in thread "main" org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "span"

...implies that you have used Select class to interact with the desired element where as the element was a <span>.

To select a value e.g. User Name from the dropdown using Selenium you can use the following solution:

  • Code Block:

      driver.get("https://ecabportal.azurewebsites.net/dashboard");
      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.name("email"))).sendKeys("[email protected]");
      driver.findElement(By.name("password")).sendKeys("NsSaNj@0205");
      driver.findElement(By.name("signIn")).click();
      new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h1[contains(., 'Dashboard')]")));
      driver.get("https://ecabportal.azurewebsites.net/user");
      new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[@id='load']")));
      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("span.select2-selection.select2-selection--single>span.select2-selection__rendered"))).click();
      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='select2-results']//li[contains(., 'User Name')]"))).click();
    
  • Browser Snapshot:

UserName


Note:

  • Always induce WebDriverWait for the elementToBeClickable() before attempting click() method when the page changes i.e. DOM changes.
  • In this particular usecase, when you browse to the desired page there is an overlay for which you need to induce WebDriverWait for the invisibilityOfElementLocated() and then invoke the required click().

7 Comments

yes its working,but i am not getting how it will works,how your taking xpath and all,can you please explain me
@divyau Check out the updated answer which now includes an explanation. Let me know if any queries.
can you repeat your Xpath related question please?
new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h1[contains(., 'Dashboard')]"))); this one i didnt get how you taken,for which element it is
As per your usecase perhaps you don't need to access the url https://ecabportal.azurewebsites.net/dashboard in the first place. Still, preserving your code trials I did invoke the url https://ecabportal.azurewebsites.net/dashboard and before performing any task on that page you need a WebDriverWait. So without any visibility to your usecase I induced a waiter for the header element with text as Dashboard
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.