Reputation: 175
I'd like to find the webelement that has as the visible text "7000118777", however I don't know how to exactly find it in the list and then click on it.
When I iterate it shows that the index is -1 and I get the error of "productList.get(-1);" - this is not the correct one.
public void findProductAndAddToCart(String product) {
List<WebElement> productList = SeleniumDriver.getDriver().findElements(By.className("bcom--txtBold"));
for (WebElement webElement : productList) {
String elements = (webElement.getAttribute("innerHTML"));
int indexOfProduct = elements.indexOf("7000118777");
System.out.println("Indeks produktu "+indexOfProduct);
}
productList.get(-1);
Upvotes: 0
Views: 312
Reputation: 816
As you have not provided a link to url or screenshot of your html, this is what i understand from your question that you want to click on a element from a list where visible text is "7000118777". I also believe that you located the elements correct i.e., your productList. Please refer the below code (Replace myDriver with your WebDriver) :
List<WebElement> productList = myDriver.findElements(By.className("bcom--txtBold"));
for (int i=0; i< productList.size();i++) {
String element=productList.get(i).getText();
if(element.equals("7000118777"))
{
productList.get(i).click();
}
}
Upvotes: 2