Monday 27 April 2015

Java Webdriver - Click using javascript

Most of the times we will be using 'WebElement' API methods to perform the 'click' operations in the automation scripts developed using webdriver.
'Click' operation can also be performed by using javascript(i.e. using 'JavascriptExecutor').
Javascript click can be used as the workaround in the automation script when ever webdriver 'WebElement' api methods are failing to perform the click operation.

Webelement holding the ObjectLocator path.
WebElement srchBttn = driver.findElement(By.name("btnG"));

Type cast the driver instance to 'JavascriptExecutor'.
JavascriptExecutor js = (JavascriptExecutor) driver;

Performs the click operation using javascript.
js.executeScript("arguments[0].click();", srchBttn);

Below code demonstrates click operation using 'Javascript'.
Sample code:
import java.util.concurrent.TimeUnit;
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;

public class JSClick {
    static WebDriver driver = null;
  
    public static void main(String[] args) throws InterruptedException {
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
        driver.get("http://www.google.com/");  
        Thread.sleep(2000);
              
        //Enter Search Text.
        WebElement txtFieldElement = driver.findElement(By.id("lst-ib"));
        txtFieldElement.sendKeys("webdriver");
        Thread.sleep(1000);
      
        //Click on Search button.
        //Click --- starts ---
            WebElement srchBttn = driver.findElement(By.name("btnG"));
            JavascriptExecutor js = (JavascriptExecutor) driver;
            js.executeScript("arguments[0].click();", srchBttn);

        //Click --- ends ---
          
        Thread.sleep(3000);                        
        driver.close();
        driver.quit();
    }  
  
}

Below code demonstrates click operation using 'WebElement' API methods of webdriver.
Sample code:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class JSClick {
    static WebDriver driver = null;
  
    public static void main(String[] args) throws InterruptedException {
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
        driver.get("http://www.google.com/");  
        Thread.sleep(2000);
              
        //Enter Search Text.
        WebElement txtFieldElement = driver.findElement(By.id("lst-ib"));
        txtFieldElement.sendKeys("webdriver");
        Thread.sleep(1000);
      
        //Click on Search button.
        //Click --- starts ---
            WebElement srchBttn = driver.findElement(By.name("btnG"));
            srchBttn.click();

        //Click --- ends ---
          
        Thread.sleep(3000);                        
        driver.close();
        driver.quit();
    }     
}

Environment:
Above script is executed on
OS: Windows 7
Brower: Firefox 37
Selenium Webdriver : 2.45.0
Java : 7

2 comments:

  1. hey buddy thanks for the post..do you have any idea hwo to work with WedDriverJS. I got couple of links about WebDriverJs. But i'm not able to follow. Please help me if you can. Thanks in advance.

    ReplyDelete
    Replies
    1. WebdriverJS - JavaScript bindings for WebDriver
      http://automation-home.blogspot.com/2015/05/webdriverjs-javascript-bindings-for-webdriver.html

      Delete