My apologies in advance if my question sounds primary, I am very new at QA and Selenium.
I am using Java and Selenium to write a test, some times I need to wait for a web element to be accessible, below is my snippet code that I used to use:
int counter = 0;
while (true) {
counter++;
boolean breakIt = false;
try {
WebElement element= driverChrome.findElement(By.xpath("bla bla"));
element.click();
breakIt = true;
} catch (Exception e) {
Thread.sleep(1000);
}
if (breakIt) {
break;
}
if (counter > 4) {
System.out.println("Failed");
tearDown();
System.exit(1);
}
}
But now somewhere I saw this:
WebDriverWait wait = new WebDriverWait(driverChrome, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("bla bla"))).click();
for sure the second one is much shorter but I do not know if it is better or not, in other words, are they different? and if yes, how? and which one is better for which purposes?