Reputation: 1142
I have been messing around with automating this options page, and since it provides a variety of options each with its own sub-options, I do not want to take the time to identify and declare all of the elements by their xpath(or CSS.. either or). So I have this reliable code that does a good job at finding the text identifier in the HTML tags.
public void selectAnOption(String option) {
List<WebElement> choice = driver.findElements(By.xpath("//div[@class='atcui-scrollPanel-wrapper']/ul/li"));
for(WebElement e : choice){
System.out.println(e.getText());
if(e.getText().equals(option)){
e.click();
break;
}
}
}
By running this I get a printout like
Mileage
Transmission
Gas Type
And so on.So boom! I know that they are identified, but my e.click() is not actually clicking. I get no errors when I start the test it just says it passed but the button was never actually clicked. Below is the HTML segment I am working with and you can see how nested it is.
Upvotes: 1
Views: 6207
Reputation: 1868
For Java 8 and above you can use:
public void selectAnOption(String option) {
List<WebElement> choice = driver.findElements(By.xpath("your_xpath"));
choice
.stream()
.filter(e->e.getText().equals(option))
.findFirst().get().click();
}
Upvotes: 1
Reputation: 1142
Fixed it.. for anyone with a similar issue, I believe it lies in the fact that when this html code was developed, there were excess spaces (used for design purposes or fitting elements during development..maybe?) so I used .contains instead of .equals. duh!!
public void selectAnOption(String option) {
List<WebElement> choice = driver.findElements(By.xpath("//div[@class='atcui-scrollPanel-wrapper']/ul/li"));
for(WebElement e : choice){
System.out.println(e.getText());
if(e.getText().contains(option)){
e.click();
break;
}
}
}
Upvotes: 0