JavaScript Artist in Selenium WebDriver

I want to use JavaScript for my script.

I created an object from a JavaScriptExecutor , but executeScript() no method. It shows an error when I use the executeScript() method.

This is the code I 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); } } 
+8
selenium-webdriver
source share
4 answers

This works for me; you had a bug on JavaScriptExecutor with an uppercase S Instead, you should have a JavaScriptExecutor with a lowercase 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); } } 

It works for me !! Please click the green mark if it is for you!

+16
source share

Please make sure you import the correct package.

Expected package for working with Java Script:

 import org.openqa.selenium.JavascriptExecutor; 

Try this package. This should solve your mistake.

+1
source share

Explanation:

Add the last jar (I use 3.0 beta selenium jar). Import the Javascript library package. Take the web driver object by sending it to the JavascriptExecutor and run any java script you want to run.

The code:

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

You can return an object from executeScript. Later you can get text from it.

 Object domain_name = js.executeScript("return document.domain"); System.out.println(domain_name.toString()); 

This way you can return values โ€‹โ€‹of any type, not just strings.

0
source share

All Articles