We Are Going To Discuss About Check if element is clickable in Selenium Java. So lets Start this Java Article.
Check if element is clickable in Selenium Java
- Check if element is clickable in Selenium Java
elementToBeClickable
is used for checking an element is visible and enabled such that you can click it.ExpectedConditions.elementToBeClickable
returnsWebElement
if expected condition is true otherwise it will throwTimeoutException
, It never returnsnull
. - Check if element is clickable in Selenium Java
elementToBeClickable
is used for checking an element is visible and enabled such that you can click it.ExpectedConditions.elementToBeClickable
returnsWebElement
if expected condition is true otherwise it will throwTimeoutException
, It never returnsnull
.
Solution 1
elementToBeClickable
is used for checking an element is visible and enabled such that you can click it.
ExpectedConditions.elementToBeClickable
returns WebElement
if expected condition is true otherwise it will throw TimeoutException
, It never returns null
.
So if your using ExpectedConditions.elementToBeClickable
to find an element which will always gives you the clickable element, so no need to check for null
condition, you should try as below :-
WebDriverWait wait = new WebDriverWait(Scenario1Test.driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@id='brandSlider']/div[1]/div/div/div/img)[50]")));
element.click();
As you are saying element.click()
passes both on link
and label
that’s doesn’t mean element is not clickable, it means returned element clicked
but may be there is no event performs on element by click action.
Note:- I’m suggesting you always try first to find elements by id
, name
, className
and other locator. if you faced some difficulty to find then use cssSelector
and always give last priority to xpath
locator because it is slower than other locator to locate an element.
Hope it helps you..:)
Original Author Saurabh Gaur Of This Content
Solution 2
There are instances when element.isDisplayed() && element.isEnabled()
will return true
but still element will not be clickable, because it is hidden/overlapped by some other element.
In such case, Exception
caught is:
org.openqa.selenium.WebDriverException: unknown error: Element is not
clickable at point (781, 704). Other element would receive the click:
<div class="footer">...</div>
Use this code instead:
WebElement element=driver.findElement(By.xpath"");
JavascriptExecutor ex=(JavascriptExecutor)driver;
ex.executeScript("arguments[0].click()", element);
It will work.
Original Author rene Of This Content
Solution 3
wait.until(ExpectedConditions)
won’t return null, it will either meet the condition or throw TimeoutException
.
You can check if the element is displayed and enabled
WebElement element = driver.findElement(By.xpath);
if (element.isDisplayed() && element.isEnabled()) {
element.click();
}
Original Author Guy Of This Content
Solution 4
There are certain things you have to take care:
- WebDriverWait inconjunction with ExpectedConditions as elementToBeClickable() returns the WebElement once it is located and clickable i.e. visible and enabled.
- In this process, WebDriverWait will ignore instances of
NotFoundException
that are encountered by default in theuntil
condition. - Once the duration of the wait expires on the desired element not being located and clickable, will throw a timeout exception.
- The different approach to address this issue are:
-
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);
-
Original Author undetected Selenium Of This Content
Conclusion
So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.