public class ClearSearch {
public static void main(String[] args) throws InterruptedException {
//Step 1:
ChromeDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(15));
driver.manage().window().maximize();
//Step 2: Hit URL
driver.get("https://rahulshettyacademy.com/seleniumPractise/#/");
List<WebElement> products = driver.findElements(By.xpath("//div[@class='products-wrapper']//h4"));
products.stream().forEach(p-> System.out.println(p.getText()));
System.out.println();
driver.findElement(By.cssSelector(".search-keyword")).sendKeys("Beet",Keys.ENTER);
Thread.sleep(Duration.ofSeconds(1));
driver.findElement(By.cssSelector(".search-keyword")).sendKeys(Keys.BACK_SPACE);
driver.findElement(By.cssSelector(".search-keyword")).sendKeys(Keys.BACK_SPACE);
products = driver.findElements(By.xpath("//div[@class='products-wrapper']//h4"));
products.stream().forEach(p-> System.out.println(p.getText()));
boolean match = products.stream().anyMatch(p->p.getText().contains("Be"));
Assert.assertTrue(match);
}
}
The execution is so quick its giving a stale element exception. But when Thread.sleep is added the error is gone. I have tried explicit wait for visibility of elements and it is not working. How to avoid Thread.Sleep()