Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Executor in Selenium WebDriver

I want to use JavaScript for my script.

I have created an object of JavaScriptExecutor, but executeScript() method is not present. It shows error when I use executeScript() method.

This is the code I have used:

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.JavascriptExecutor;


public class GetDomain_JS {

    public static void main(String[] args) {
        WebDriver driver=new FirefoxDriver();
        driver.get("http://only-testing-blog.blogspot.in/2013/11/new-test.html");
        driver.manage().window().maximize();

        System.out.println(driver.getCurrentUrl());

        JavaScriptExecutor js=(JavaScriptExecutor) driver;

        String domain_name=(String) js.executeScript("return document.domain");

                System.out.println(doamin_name);

    }
}
like image 832
Ramarajan Avatar asked Jun 07 '14 15:06

Ramarajan


1 Answers

It works for me; you had a mistake on JavaScriptExecutor with upper case S. Instead, you should have javascriptExecutor with lower case s.

Try this code:

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;


public class GetDomain_JS {

public static void main(String[] args) {
    WebDriver driver=new FirefoxDriver();
    driver.get("http://only-testing-blog.blogspot.in/2013/11/new-test.html");
    driver.manage().window().maximize();

    System.out.println(driver.getCurrentUrl());

    JavascriptExecutor js=(JavascriptExecutor) driver;

    String domain_name=(String) js.executeScript("return document.domain");

            System.out.println(domain_name);

}
}

This works for me!! Please thumps up if it does for you!

like image 165
pelican Avatar answered Oct 27 '22 23:10

pelican