Reputation: 27
Here i am creating a common method in class Common.java for selecting elements based on visibleText
.And in another class(NewTest.java) I am calling the value of sellectByVisibleText
. But when I am debugging this program it shows
error found :
org.openqa.selenium.support.ui.UnexpectedTagNameException:
Element should have been "select" but was "input". so how can I select employee from dropdown list??
1) Common.java
public static String selectByVisibletext(String xPath,String inputData, WebDriver driver) {
WebElement webElement = driver.findElement(By.xpath(xPath));
try {
System.out.println(inputData);
System.out.println(xPath);
System.out.println(driver);
Select selectBox = new Select(webElement);
selectBox.selectByVisibleText(inputData);
}
catch (Exception e) {
System.out.println("error found : "+e);
}
return inputData;
}
1) NewTest.java
String search = "//input[@class='select2-search__field']";
public void employee() {
Common.selectByVisibletext(search,"Employee", driver);
}
Upvotes: 1
Views: 1049
Reputation: 29382
If the drop down is not made of select options HTML tags , then Selenium select class will not work.
In that case, you will have to store all options in a list. Then you can Iterate through the list and put one condition. Something like.
First click on drop down.
driver.findElement(By.id("select2-acc_roles-container")).click();
Then store every options in a list like this :
List<WebElement> options = driver.findElements(By.xpath("xpath for all option. This xpath should contain every options"));
for(WebElement option : options) {
if(option.getText().trim().contains("What you want to select from drop down")) {
option.click();
}
}
Hope this will help.
Upvotes: 0
Reputation: 573
The Select class only works with SELECT tags that have option fields. If there are no option fields they would be rejected. If you can post the HTML that would be great. And also you are passing the string holding an INPUT tag into your method. You should pass in an XPATH which actually holds a SELECT tag.
Clearly the below(which you are passing into the method) is a XPATH to an Input field and not a Select field:
String search = "//input[@class='select2-search__field']";
Upvotes: 0
Reputation: 1275
You can use something like that if the tag is not a "select" tag :
public static String selectByVisibletext(String xPath,String inputData, WebDriver driver) {
WebElement webElement = driver.findElement(By.xpath(xPath));
try {
System.out.println(inputData);
System.out.println(xPath);
System.out.println(driver);
Select selectBox = new Select(webElement);
selectBox.selectByVisibleText(inputData);
}
catch (Exception e) {
if (e.getMessage().contains("UnexpectedTagNameException")) {
List<WebElement> dropDown = driver.findElements(By.xpath(xPath));
dropDown.forEach(dropElement -> {
if (dropElement.getAttribute("innerText").equals(inputData)) {
dropElement.click();
}
});
}
}
return inputData;
}
The concept is to collect the elements using your selector in a List and then find a match for your desired selectable element and then click on it.
First click on "//input[@class='select2-search__field']" and then find a selector for all dropdown elements and then get them as a list of WebElements and select by matching your text.
Upvotes: 1