diwakar reddy
diwakar reddy

Reputation: 3

How to select different dropdowns which has same name in selenium webdriver

This select fields has same name

Error showing as

org.openqa.selenium.remote.RemoteWebElement cannot be cast to org.openqa.selenium.support.ui.Select

Here is my code:

List<WebElement> ref = driver.findElements(By.name("customerBean.relationCd"));
System.out.println("reference dropdowns " + ref.size());
((Select) ref.get(0)).selectByIndex(18);
((Select) ref.get(1)).selectByIndex(18);

Upvotes: 0

Views: 683

Answers (1)

Manu
Manu

Reputation: 2301

The issue with your code is that you have type-casted WebElement object to the Select object.

((Select) ref.get(0)).selectByIndex(18);

That is not how it can be done. The Select object should be used separately as defined with webelement as parameter and not by type casting.

List<WebElement> ref = driver.findElements(By.name("customerBean.relationCd"));
System.out.println("reference dropdowns " + ref.size());
Select s;

s = new Select(ref.get(0));
s.selectByIndex(18);

s = new Select(ref.get(1));
s.selectByIndex(18);

Hope that helps you, let me know if you have more query.

Upvotes: 1

Related Questions