Using JavaScript in Selenium WebDriver


We can use Javascript to perform actions Using selenium Webdriver. By using ‘JavascriptExecutor’ we can perform these actions

Get Title

driver.get(“http://google.co.in/”);
JavascriptExecutor js = (JavascriptExecutor) driver;
String title = (String) js.executeScript(“return document.title”);
System.out.println(“title : ” + title);
OUTPUT | title : Google

3| Get Domain name

driver.get(“http://google.co.in/”);
JavascriptExecutor js = (JavascriptExecutor) driver;
String domain = (String) js.executeScript(“return document.domain”);
System.out.println(“domain : ” + domain);
OUTPUT | domain : http://www.google.co.in

4| Get URL

driver.get(“http://google.co.in/”);
JavascriptExecutor js = (JavascriptExecutor) driver;
String URL = (String) js.executeScript(“return document.URL”);
System.out.println(“Full URL : ” + URL);
OUTPUT | Full URL : https://www.google.co.in/

5| Get Attribute | Text

driver.get(“http://google.co.in/”);
Object exampleDiv = ((JavascriptExecutor) driver).executeScript(“return document.getElementById(‘main’);”);
String name = ((WebElement) exampleDiv).getAttribute(“class”);
System.out.println(name);
OUTPUT | content

6| Last Modified

driver.get(“http://google.co.in/”);
JavascriptExecutor js = (JavascriptExecutor) driver;
String lastModified = (String) js.executeScript(“return document.lastModified”);
System.out.println(“lastModified : ” + lastModified);
OUTPUT | lastModified : 01/29/2014 14:56:46


7| Ready state

driver.get(“http://google.co.in/”);
JavascriptExecutor js = (JavascriptExecutor) driver;
String readyState = (String) js.executeScript(“return document.readyState”);
System.out.println(“readyState : ” + readyState);
OUTPUT | readyState : complete

8| Click()

WebElement element = driver.findElement(By.id(“Value”));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript(“arguments[0].click();”, element);
OR |
WebElement element = driver.findElement(By.id(“Value”));
trigger(“arguments[0].click();”, element);

public void trigger(String script, WebElement element) {
((JavascriptExecutor) driver).executeScript(script, element);
}
OR |
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(“window.document.getElementById(‘Value’).click()”);

Advertisements

6 thoughts on “Using JavaScript in Selenium WebDriver

  1. Anonymous June 4, 2015 / 3:36 pm

    Very Nice.Thanks dear.

    Like

  2. Anonymous June 4, 2015 / 3:37 pm

    Good to know these stuff.

    Like

  3. Anonymous June 4, 2015 / 3:38 pm

    How to Work with excel using javascript ??

    Like

  4. Anonymous June 9, 2015 / 6:45 pm

    Nice article !

    Like

    • charan163 June 9, 2015 / 7:25 pm

      ThankYou

      Like

Leave a Reply

Please log in using one of these methods to post your comment:

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