Executing javascript using Selenium WebDriver

Using webdriver sometimes we need to run javascript code dircetly from our script.

In one of the previous post we have discussed how to break catcha on webpage . To break captcha we need to run javascript code directly from our script .So it is important to know how to run javacript code using WebDriver. Here are the steps:

1.Cast the WebDrier instance to a  JavascriptExecutor

WebDriver driver;
JavascriptExecutor js = (JavascriptExecutor) driver;

2.Use executeScript method to run the script

js.executeScript("return document.title");

Here is the sample script using TestNG.

import java.util.ArrayList;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Reporter;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class ExecuteJavascript {

WebDriver driver;

@BeforeTest
public void start(){
driver = new FirefoxDriver();
}

@Test
public void javaScriptExec(){

driver.get("http://duckduckgo.com/");
JavascriptExecutor js=(JavascriptExecutor) driver;

String readyState=(String)js.executeScript("return document.readyState");
System.out.println("readyState : "+readyState);

String title=(String)js.executeScript("return document.title");
System.out.println("title : "+title);

String domain=(String)js.executeScript("return document.domain");
System.out.println("domain : "+domain);


String lastModified=(String)js.executeScript("return document.lastModified");
System.out.println("lastModified : "+lastModified);

String URL=(String)js.executeScript("return document.URL");
System.out.println("Full URL : "+URL);

String error=(String) ((JavascriptExecutor) driver).executeScript("return window.jsErrors");
System.out.println("Windows js errors : "+error);

}

@AfterTest
public void stop(){
driver.quit();
}

}

If you run the above program then the output will come like below :

readyState  : complete
title  : DuckDuckGo
domain  : duckduckgo.com
lastModified  : 02/02/2013 13:11:42
Full URL  : https://duckduckgo.com/
Windows js errors  :   null

Reference :

http://code.google.com/p/selenium/wiki/WebDriverJs#Getting_Started
http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions#Q:_How_do_I_execute_Javascript_directly?
http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/JavascriptExecutor.html

About these ads

3 thoughts on “Executing javascript using Selenium WebDriver

  1. The line checking 'readyState' – either this is a brilliant and simple solution for checking if the document is "ready for scraping" (how come I haven't thought of that?), or there is something missing here (since I have not encountered this in any other forum, including on StackOverflow).Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s