Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if element is clickable in Selenium Java

I'm new to Selenium and need to check if element is clickable in Selenium Java, since element.click() passes both on link and label.

I tried using the following code, but it is not working:

WebDriverWait wait = new WebDriverWait(Scenario1Test.driver, 10);

if(wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@id='brandSlider']/div[1]/div/div/div/img)[50]")))==null)
like image 446
Sandeep Krishnappa Avatar asked Jul 12 '16 11:07

Sandeep Krishnappa


People also ask

How do you check if a Webelement is clickable or not?

We can check if the element is clickable or not in Selenium webdriver using synchronization. In synchronization, there is an explicit wait where the driver waits till an expected condition for an element is met. To verify, if the element can be clicked, we shall use the elementToBeClickable condition.

How do I check if a link is enabled in Selenium?

Using web console of your browser inspect element to check if there is any attribute to disable the link using style, class etc. String temp = selenium. getAttribute(locator@attribute); Now you can verify the value of temp.

Why is Selenium element not clickable?

This is because the element is present in DOM, but its position is not fixed on the page. Adding the explicit wait. The webdriver can wait till the expected condition - visibilityOf(webdriver shall wait for an element in DOM to be visible). Adding the explicit wait.

What is contextClick ()?

Move to Element: contextClick() method first performs mouseMove to the middle of the element location. This function performs the right click at the middle of the web element.


Video Answer


3 Answers

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..:)

like image 116
Saurabh Gaur Avatar answered Oct 19 '22 18:10

Saurabh Gaur


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.

like image 25
user8639449 Avatar answered Oct 19 '22 18:10

user8639449


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();
}
like image 10
Guy Avatar answered Oct 19 '22 20:10

Guy