Element not clickable at point Selenium Webdriver


Element not clickable at point (x, y)


This issue normally occurs if the target element is not clickable, which means that the element is not in the state of user interaction. This is due to the following reason
A.) Element is in list and is currently not in the view-able area of the browser

How To Fix

  1. (Case A)Scroll the page so the element comes in the viewable area of the browser
    for example:

    // Get the element
    WebElement targetElement = driver.findElement(By.id("itemid"));
    // Create the JavascriptExecutor object
    JavascriptExecutor jse = (JavascriptExecutor)driver;
    // Scroll down the page to the element with JavaScript
    jse.executeScript("window.scrollTo(0,'+elementToClick.getLocation().y+')");// Click on the element
  2. (General Case) This method doesn’t care about the current state of the target element. No matter if the element is at the bottom of the page and requires scroll to access it.
    So this method is a bit hackish since from the user point of view you cannot click(or interact with) an element if the element is present at the bottom of the page and requires some scrolling.

    In this method we use JavaScript to perform the click. This method will succeed as long as the element is present in the page.

    Here is how to do this:

    //Get the element
    WebElement we = driver.findElement(By.id("elementid"));
    // Create the JSExecutor object
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    // Perform the click with javascript
    executor.executeScript("arguments[0].click();", we);

Selenium WebDriver Element is not visible error

Element is not visible error:

This error occurs when the target element is hidden below some other element (something like a dialog box) or it is hidden by the CSS in the page. In this case we have two options:

  1. Remove or hide the other element so that the target element is visible and the perform events on it.
    For this get the element

    WebElement we = driver.findElement(By.id("elementid"));
    //Hide the element
    ((JavascriptExecutor)driver).executeScript("arguments[0].style.display='none'", we);

    and remove it or hide it using JavaScript or by selenium.

  2. In case visibility of the element is not important and still some action needs to performed then with the help of JavaScript webdriver can perform the click event on the target element and the visibility of the element will be ignored.Here is how we can do this:
    WebElement we = driver.findElement(By.id("elementid"));
    // JSExecutor 
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    // JS click 
    executor.executeScript("arguments[0].click();", we);

    This method works because the JavascriptExecutor object does not care whether the element is visible or not, but while doing it normal way the element needs to be visible.

Handling tinyMCE editor with Selenium Webdriver

Here is how to use Selenium Webdriver with tinyMCE:

The tinyMCE editor creates its own iframe, so before selenium is able to perform any task or action on it, it has to first switch the control to the iframe and then start execution.
Here is how to switch this:

// Select the iframe
// select the editor nad do the input 
 driver.findElement(By.id("tinymce")).sendKeys("Dummy text");

Now after the WebDriver is all done with its actions on the editor it needs to switch back to the default window to continue with the rest of the script. Below is the sample code to switch back to the default window:

// switch back to the default window 

Hope this helps. Please let us know in case of any issues.

Selenium Explicit and Implicit Waits


To wait means having the task to stop execution for a certain amount of time. After the the time is elapsed the execution continues with he next steps. Selenium API provides two flavours of waits: Explicit Wait and Implicit Wait.

Explicit Wait

Explicit Wait is the code to stop execution for a certain time until an expected condition is met. The expected condition may be the presence of any element of the visibility of the element. As soon as the expected condition is evaluated to be true the execution resumes. For instance, if an explicit wait of 10 seconds is set and we want to stop the execution until an element is present on the page, the WebDriver will on every 500 miliseconds check to see if the element is present on the page or not. If the element is found(i.e., the condition becomes true before 10 seconds), the execution will resume. In case the condition is not met and the time(10 seconds in this case) has elapsed the TimeoutException is thrown. There are few convenience method provided by the API to implement the expected conditions.

Using presenceOfElementLocated

WebDriver driver = new FirefoxDriver();
WebElement element = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("some_element_id"))); //Element is present

Using elementToBeClickable

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("some_element_id"))); //Element to be displayed and enabled to be clicked

Another way to implement the explicit wait is to use the Thread.sleep() (not recommended) method, which will cause the current Java Thread to stop execution for a certain amount of time. Note that this is not a preferred way to implement explicit wait as it can cause the task to run into deadlock and other threading related issues.

Implicit Wait

Implicit wait is a way to tell the driver to look into the DOM for an element for certain amount of time if the element is not available immediately. By default the time for the implicit wait is 0 seconds. This implicit time can be changed and once changed it will be used till the life of WebDriver object. Of course this can again changed later the same way it was set earlier. Implicit wait especially usefull when the elements are created dynamically on the page, a modal box, for example. Below is an example of setting the implicit time wait.

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement element = driver.findElement(By.id("some_element_id"));

In the above example please note that the implicitlyWait() method takes the two arguments the first is the amount of time and the second is the unit of time. Hence the above example will set the implicit wait to 10 seconds. This way the findElement() will not return immediately if the element is not found, instead it will wait for 10 seconds for the element to be present and in between the waiting it will keep polling (after every 500 milliseconds) the DOM for the element. As soon as the element is present it will be returned.