Shambhavi Srivastava
Shambhavi Srivastava

Reputation: 21

I am trying to click the first element from a list but the compiler throws exception every time

I need to click the first element from the list.

I also tried type-casting the element before clicking but it is throwing an exception as well.

wait = new WebDriverWait(driver,60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(ClientUICommon.getClientUIPaths().getProperty("BugFRList"))));
System.out.println("Bug to be clicked ");
ClientUICommon.sleep(3000);
((WebElement) driver.findElements(By.cssSelector(ClientUICommon.getClientUIPaths().getProperty("BugFRList")))).click();

Upvotes: 0

Views: 52

Answers (1)

Fenio
Fenio

Reputation: 3635

The problem is in the below code:

((WebElement) driver.findElements(By.cssSelector(ClientUICommon.getClientUIPaths().getProperty("BugFRList")))).click();

You see, the method findElements does not return WebElement object. It returns the List of WebElements. List<WebElement> to be exact.

What you basically did was clicking on the list, not it's element:

driver.findElements(...).click();

Which will throw compilation error, because List<> does not have method click(). In order to click on the first element of the list you should use get method with index argument like this:

list.get(0);

The above will return single WebElement

Full code:

List<WebElement> elementList = driver.findElements(By.cssSelector(ClientUICommon.getClientUIPaths().getProperty("BugFRList"))));
WebElement firstElement = elementList.get(0);
firstElement.click();

Upvotes: 1

Related Questions