There are certain things you have to take care:
NotFoundException
that are encountered by default in the until
condition.To invoke click()
as soon as the element is returned, you can use:
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@id='brandSlider']/div[1]/div/div/div/img)[50]"))).click();
To simply validate if the element is located and clickable, wrap up the WebDriverWait in a try-catch{}
block as follows:
try {
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@id='brandSlider']/div[1]/div/div/div/img)[50]")));
System.out.println("Element is clickable");
}
catch(TimeoutException e) {
System.out.println("Element isn't clickable");
}
If WebDriverWait returns the located and clickable element but the element is still not clickable, you need to invoke executeScript()
method as follows:
WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@id='brandSlider']/div[1]/div/div/div/img)[50]")));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);