Can someone please help me out with this code. I'm getting NoSuchElement exception when I'm using streams. The foreach()
alternative(Commented) is working fine. I want to know why it is throwing the exception if implemented with streams. Here is the code for your reference:
public class Test {
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "E:\\Sajidh\\WebDriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://rahulshettyacademy.com/seleniumPractise/#/");
WebElement product = driver.findElements(By.cssSelector("div.product")).stream().filter(p->p.findElement(By.cssSelector("h4.product-name")).getText().contains("Potato")).collect(Collectors.toList()).get(0);
// WebElement product = null;
// for(WebElement p : driver.findElements(By.cssSelector("div.product")))
// {
// if(p.findElement(By.cssSelector("h4.product-name")).getText().contains("Potato"))
// {
// product = p;
// break;
// }
// }
System.out.println(product.findElement(By.cssSelector("h4.product-name")).getText());
System.out.println(product.findElement(By.cssSelector("input.quantity")).getText());
System.out.println(product.findElements(By.cssSelector("div.product-action button:nth-child(1)")).size());
product.findElement(By.cssSelector("div.product-action button:nth-child(1)")).click();
}
}