Home > Back-end >  UnexpectedTagNameException: Element should have been "select" but was "a" while
UnexpectedTagNameException: Element should have been "select" but was "a" while

Time:02-10

While trying to get the menu list, I'm getting this error message:

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

Here below is the code:

public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", "D:\\selenium files\\chromedriver_win32_new\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.tutorialspoint.com/tutor_connect/index.php");
        
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);    
        
        WebElement ele = driver.findElement(By.xpath("//*[@id=\"logo-menu\"]/div/div[1]/div/a"));
        
        Select s = new Select(ele);
        
    //getting list of menu
         List <WebElement> op = s.getOptions();
          int size = op.size();
          for(int i =0; i<size ; i  ){
             String options = op.get(i).getText();
             System.out.println(options);
          }
    }
}

CodePudding user response:

That is because the element you are trying to cast is a link tag and not a select tag. You need to give the Xpath or CSS of the correct Select element and then cast it from WebElement into a Select ojbect.

In the example you are using there is not real selector, you first need to click on the buttons that says "Categories" and later take the options that appear:

WebElement button = driver.findElementByCSS("div[class='mui-dropdown']");
button.click();
WebElement SelectObj = driver.findElementByCSS("ul[class*='mui--is-open']");
Select s = new Select(SelectObj);

CodePudding user response:

The desired element is not a <select> element but a <ul> element. Once you click on the <a> element then only the classname mui--is-open is appended to the desired <ul> element.

tutorialspoint


Solution

So to get the contents of the dropdown menu you need to induce WebDriverWait for the visibilityOfAllElementsLocatedBy() and you can use Java8 stream() and map() and you can use either of the following Locator Strategies:

  • Using cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.mui-btn.mui-btn--primary.categories"))).click();
    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("ul.mui-dropdown__menu.cat-menu.mui--is-open a"))).stream().map(element->element.getText()).collect(Collectors.toList()));
    
  • Using xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='mui-btn mui-btn--primary categories']"))).click();
    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//ul[@class='mui-dropdown__menu cat-menu mui--is-open']//a"))).stream().map(element->element.getText()).collect(Collectors.toList()));
    

References

You can find a couple of relevant detailed discussions in:

  • Related