12

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

    }
}
1
  • 2
    I should use JavascriptExecutor instead of JavaScriptExecutor. Commented Jun 8, 2014 at 11:54

4 Answers 4

19

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!

Sign up to request clarification or add additional context in comments.

3 Comments

What is the difference? Corrected typo in System.out.println(doamin_name);?
Its works now, The mistake is: I have created instance of JavaScriptExecutor, JavascriptExecutor (lowercase S) is the correct one.
JavascriptExecutor js=(JavascriptExecutor) driver; I didn't understand this line, JavascriptExecutor is an interface, so we are casting driver object to JavascriptExecutor??
2

Please make sure you have imported the correct package.

Expected package for working with Java Script:

import org.openqa.selenium.JavascriptExecutor;

Try this package. This should solve your error.

Comments

1

Explanation:

Add latest jar (I'm using 3.0 beta selenium jar). Import Javascript library package. Take web driver object by casting to JavascriptExecutor and run whatever java script you want to run.

Code:

import com.thoughtworks.selenium.webdriven.JavascriptLibrary;
Object ob = ((JavascriptExecutor) webDriver()).executeScript("return document.domain").toString();
System.out.println(ob);

2 Comments

Your answer certainly is worth a little explanation. This would add searchable content, so that other users will find it and benefit from our work.
Agreed, i thought there are many answers so all have little idea what code says. Added some more info.
0

You can return an Object from executeScript. Later you can get the text out of it.

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

System.out.println(domain_name.toString());

In this way, you can return values of any type and not just string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.