package Practice;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class Vistair2 {
    public void seeProducts() throws InterruptedException{

System.setProperty("webdriver.chrome.driver", "/Users/dominiclee/Documents/workspace/chromedriver 4");
WebDriver driver = new ChromeDriver();

//Create action builder instance by passing WebDriver instance
Actions builder = new Actions(driver);
WebElement menuElement = driver.findElement(By.linkText("SOLUTIONS"));

Select dropDown = new Select (driver.findElement(By.linkText("SOLUTIONS")));
List<WebElement> e = dropDown.getOptions();
int itemsCount = e.size();



When I run this in Selenium I get the following error: Element should have been "select" but was "a"

Do I want to find the size of the drop-down menu?

Here is the html:

enter image description here

Update from mistaken answer:

I have also tried classname:

Select dropDown = new Select (driver.findElement(By.className("dropdown-menu")));

but then I get the error:

The Element should have been "select" but was "ul"

What locator should I be using?

  • 1
    Update your question with html code of what you're trying to locate. – Alexey R. Sep 3 '18 at 22:54

You're using linktext locator, hence it locates a link (a). Then you're trying to wrap your found link with Select element which obviously causes an error as Selenium is trying to work with found link as with a list.

What you need to do is to use the proper locator for your Select element.

UPD: According to the HTML you have provided what you have is not a select element but just a list. Selenium Select class is a wrapper for elements like this:

enter image description here

Even if you have a drop-down implemented as a list, you need to use a regular approach.

  1. Prepare a locator that would locate all elements you need
  2. Invoke driver.findElements(..) with that locator
  3. Count the number of item in returned List<WebElement>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.