0

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();
}
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

0

To print the textContent of all the elements you need to induce WebDriverWait for the visibilityOfAllElementsLocatedBy() and you can use Java8 stream() and map() and you can use either of the following Locator Strategies:

  • cssSelector:

    import java.util.stream.Collectors;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    System.setProperty("webdriver.chrome.driver", "C:\\WebDrivers\\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--start-maximized");
    WebDriver driver = new ChromeDriver(options);
    driver.get("https://rahulshettyacademy.com/seleniumPractise/#/");
    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div.product h4.product-name"))).stream().map(element->element.getText()).collect(Collectors.toList()));
    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[@class='product']//h4[@class='product-name']"))).stream().map(element->element.getText()).collect(Collectors.toList()));
    driver.quit();
    
  • Console Output:

    [Brocolli - 1 Kg, Cauliflower - 1 Kg, Cucumber - 1 Kg, Beetroot - 1 Kg, Carrot - 1 Kg, Tomato - 1 Kg, Beans - 1 Kg, Brinjal - 1 Kg, Capsicum, Mushroom - 1 Kg, Potato - 1 Kg, Pumpkin - 1 Kg, Corn - 1 Kg, Onion - 1 Kg, Apple - 1 Kg, Banana - 1 Kg, Grapes - 1 Kg, Mango - 1 Kg, Musk Melon - 1 Kg, Orange - 1 Kg, Pears - 1 Kg, Pomegranate - 1 Kg, Raspberry - 1/4 Kg, Strawberry - 1/4 Kg, Water Melon - 1 Kg, Almonds - 1/4 Kg, Pista - 1/4 Kg, Nuts Mixture - 1 Kg, Cashews - 1 Kg, Walnuts - 1/4 Kg]
    
  • xpath:

    import java.util.stream.Collectors;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    System.setProperty("webdriver.chrome.driver", "C:\\WebDrivers\\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--start-maximized");
    WebDriver driver = new ChromeDriver(options);
    driver.get("https://rahulshettyacademy.com/seleniumPractise/#/");
    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div.product h4.product-name"))).stream().map(element->element.getText()).collect(Collectors.toList()));
    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[@class='product']//h4[@class='product-name']"))).stream().map(element->element.getText()).collect(Collectors.toList()));
    driver.quit();
    
  • Console Output:

    [Brocolli - 1 Kg, Cauliflower - 1 Kg, Cucumber - 1 Kg, Beetroot - 1 Kg, Carrot - 1 Kg, Tomato - 1 Kg, Beans - 1 Kg, Brinjal - 1 Kg, Capsicum, Mushroom - 1 Kg, Potato - 1 Kg, Pumpkin - 1 Kg, Corn - 1 Kg, Onion - 1 Kg, Apple - 1 Kg, Banana - 1 Kg, Grapes - 1 Kg, Mango - 1 Kg, Musk Melon - 1 Kg, Orange - 1 Kg, Pears - 1 Kg, Pomegranate - 1 Kg, Raspberry - 1/4 Kg, Strawberry - 1/4 Kg, Water Melon - 1 Kg, Almonds - 1/4 Kg, Pista - 1/4 Kg, Nuts Mixture - 1 Kg, Cashews - 1 Kg, Walnuts - 1/4 Kg]
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Hi Debanjan, Thank you for your help. The printing is working fine for me. I have problem with the streams statement on line no.5. It is returning NoSuchElementException. Do you have any idea why it is happening? – Sajidh15Kareem Jan 17 '21 at 07:15