SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

How to wait for element to be clickable in selenium webdriver using explicit 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.

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")));

Here above syntax 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. 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.

Let me give you practical example for the element to be clickable.
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 = new FirefoxDriver();
 
 @Before
 public void beforetest() {
  driver.manage().window().maximize();
  driver.get("http://only-testing-blog.blogspot.in/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 wait for element to be clickable in selenium webdriver in your eclipse with junit. Click here to view different posts on how to use junit with eclipse for your webdriver test.

8 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
  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