Reputation: 133
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);
}
}
Upvotes: 12
Views: 49679
Reputation: 6224
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!
Upvotes: 19
Reputation: 4043
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.
import com.thoughtworks.selenium.webdriven.JavascriptLibrary;
Object ob = ((JavascriptExecutor) webDriver()).executeScript("return document.domain").toString();
System.out.println(ob);
Upvotes: 1
Reputation: 5819
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.
Upvotes: 0
Reputation: 1485
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.
Upvotes: 2