How to wait for element to be clickable in selenium webdriver using explicite wait

In my previous post, We have seen how to wait implicitly in selenium webdriver software testing tool. Let me remind you one thing is implicit wait will be applied to all elements of test case by default while explicit will be applied to targeted element only. This is the difference between implicit wait and explicit wait. Still I am suggesting you to use implicit wait in your test script. You need selenium wait for element to be clickable when element is not clickable on page takes long time to load all elements. Explicate wait is useful for selenium wait until element is clickable.

If you knows, In selenium IDE software testing tool we can use "waitForElementPresent" or "verifyElementPresent" to wait for or verify that element is present or not on software web application page. In selenium webdriver, we can do same thing using explicit wait of elementToBeClickable(By locator). Full syntax is as bellow.

Read more tutorials on selenium WebDriver @Tutorials Part 1 and @Tutorials Part 2.

WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#submitButton")));

Above wait for element to be clickable selenium java will wait till 15 seconds to become targeted element(#submitButton) clickable if it is not clickable or not loaded on the page of software web application. It will check if element is clickable in selenium test script. As soon as targeted element becomes clickable on the page of software web application, webdriver will go for perform next action. You can increase or decrease webdriver wait time from 15.

Selenium wait until element is clickable Example

Let me give you practical example for wait until clickable selenium java.
package junitreportpackage;

import java.util.concurrent.TimeUnit;
import java.io.FileInputStream;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;


public class Mytest1 {
 WebDriver driver = null;
 
 @Before
 public void beforetest() {
  System.setProperty("webdriver.gecko.driver", "D:\\Selenium Files\\geckodriver.exe");
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  driver.get("http://only-testing-blog.blogspot.com/2013/11/new-test.html");
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
 }
 
 @After
 public void aftertest() {
  driver.quit();
  
 }
 
 @Test
  public void test () 
   {  
    
    driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("My Name");
    //Wait for element to be clickable
    WebDriverWait wait = new WebDriverWait(driver, 15);
    wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#submitButton")));

    driver.findElement(By.cssSelector("#submitButton")).click();
   }
 public void HighlightMyElement(WebElement element) { 
  for (int i = 0; i < 10; i++) 
  { 
   JavascriptExecutor javascript = (JavascriptExecutor) driver;
   javascript.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "color: orange; border: 4px solid orange;");
   javascript.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "color: pink; border: 4px solid pink;");
   javascript.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "color: yellow; border: 4px solid yellow;");
   javascript.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, ""); 
   } 
  } 
 }

Run above given example of selenium wait until element is clickable in your eclipse with junit. It will show you how to wait for element to be clickable in selenium java. Click here to view different posts on how to use junit with eclipse for your webdriver test.

11 comments:

  1. Its possible to highlight the element and wait until capture the screenshots ??

    ReplyDelete
  2. Hi, Can u please explain what is use of for loop here ?

    ReplyDelete
  3. can anyone explains why we have used highlightmyelement func()

    ReplyDelete
  4. I have some questions:
    Since you have the code below which will wait for 15seconds
    ->driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

    Will this add up on the code below?
    ->WebDriverWait wait = new WebDriverWait(driver, 15);
    wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#submitButton")));

    If submit button is not yet clickable, does it mean it will wait for 15seconds?
    I assume that because as I understand implicitWait is one time setup.
    Is it really recommended to use implicit rather than explicit?
    Implicit will make your testing time longer...

    ReplyDelete
    Replies
    1. implicitlyWait will wait till given time(15 seconds) if targeted element not found on page. It will apply to whole script. So it is recommended to use.

      We use explicit wait in special conditions only like some element is taking too much time to be visible, waiting for alert or some text etc.. Hope this will clear your doubt.

      Delete
  5. HI i have some doubt for example we given 15 seconds in implicit wait, the element is clickable with in the 10 seconds. It is wait until 15 seconds even though the element is click able before 15 seconds or it it will continue to next.

    Same as Explicit

    can u give the answer please i faced this question in one of the interview

    ReplyDelete
    Replies
    1. HI ,implicit wait will wait upto 15 seconds,when selenium finds element at 10 seconds , it will continue the process .
      Eplicit wait : will wait for 15 seconds no matter if element present or not (if the element present also in 10th seconds it should wait for 15 seconds .)

      Delete
    2. HI ,implicit wait will wait upto 15 seconds,when selenium finds element at 10 seconds , it will continue the process .
      Eplicit wait : will wait for 15 seconds no matter if element present or not (if the element present also in 10th seconds it should wait for 15 seconds .)

      Delete
    3. if implicit wait finds the element withing 10 seconds it will start process and will ignore remaining 5 seconds.
      And if you have applied explicit wait also then explicit wait will also try to find the element even though implicit wait already have found but , you have written the line of code for explicit wait and the line of code will execute and explicit wait will try to find and lets say explicit wait found the element in 2 seconds then code will execute...Reason why explicit wait will still try to find the element because it will check and and make sure it has found the element and then code will execute so it will also have its own sureity by checking.

      Delete
  6. @tulasireddy: Im sure it will click on button if its clickable within 10 seconds

    ReplyDelete
  7. i have used same code but it's not works

    ReplyDelete