55

I want to use JavaScript with WebDriver (Selenium 2) using Java.

I've followed some a guide and on Getting Started page: there is an instruction at 1st line to run as:

$ ./go webdriverjs 

My question: From which folder/location the command mentioned above will be run/executed?

2
  • 4
    Do you want to write your tests in JavaScript, or do you want to use Javascript commands in your test (written in Java)?CommentedJul 11, 2012 at 11:14
  • I want to use Javascript commands in my test (written in Java)CommentedJul 17, 2012 at 4:46

8 Answers 8

131

Based on your previous questions, I suppose you want to run JavaScript snippets from Java's WebDriver. Please correct me if I'm wrong.

The WebDriverJs is actually "just" another WebDriver language binding (you can write your tests in Java, C#, Ruby, Python, JS and possibly even more languages as of now). This one, particularly, is JavaScript, and allows you therefore to write tests in JavaScript.

If you want to run JavaScript code in Java WebDriver, do this instead:

WebDriver driver = new AnyDriverYouWant(); if (driver instanceof JavascriptExecutor) { ((JavascriptExecutor)driver).executeScript("yourScript();"); } else { throw new IllegalStateException("This driver does not support JavaScript!"); } 

I like to do this, also:

WebDriver driver = new AnyDriverYouWant(); JavascriptExecutor js; if (driver instanceof JavascriptExecutor) { js = (JavascriptExecutor)driver; } // else throw... // later on... js.executeScript("return document.getElementById('someId');"); 

You can find more documentation on this here, in the documenation, or, preferably, in the JavaDocs of JavascriptExecutor.

The executeScript() takes function calls and raw JS, too. You can return a value from it and you can pass lots of complicated arguments to it, some random examples:

1.

 // returns the right WebElement // it's the same as driver.findElement(By.id("someId")) js.executeScript("return document.getElementById('someId');"); 
  1.  // draws a border around WebElement WebElement element = driver.findElement(By.anything("tada")); js.executeScript("arguments[0].style.border='3px solid red'", element); 
  2.  // changes all input elements on the page to radio buttons js.executeScript( "var inputs = document.getElementsByTagName('input');" + "for(var i = 0; i < inputs.length; i++) { " + " inputs[i].type = 'radio';" + "}" ); 
14
  • 5
    @RiponAlWasim If that function is embedded to your page, just do js.executeScript("showAlert()");. If it isn't, do js.executeScript("function showAlert() { alert('success'); }; showAlert()");CommentedJul 17, 2012 at 16:04
  • 2
    Thanks a lot. js.executeScript("function showAlert() { alert('success'); }; showAlert()"); is worked well. Alternatively, I want to write that JS code/function in my Java code separately and want to call JS function as js.executeScript("showAlert()"); What's the way/syntax of writing JS code/function in Java code separately?CommentedJul 18, 2012 at 4:08
  • 3
    nice. Again thanks. It is working very well & I have got the exact solutionCommentedJul 19, 2012 at 3:24
  • 1
    @CodeEnthusiastic Yes. Or almost, in case you misunderstood a small detail. The first argument is the script, any additional argument (second, third, etc.) is passed to the script (and is accessible via the magic arguments variable).CommentedJul 6, 2013 at 16:12
  • 1
    @Srk95 Hello! I'm sorry, this post is 9 years old now and I have not been using WebDriver or any other similar technology for more tha 7 years now. I simply don't know anymore. It's likely that because of the cross-domain policy you will not be able to do that at all? I found this old answer for injecting jQuery, perhaps it would still work after all those years: sqa.stackexchange.com/questions/2921/…CommentedJul 13, 2021 at 9:46
5

JavaScript With Selenium WebDriver

Selenium is one of the most popular automated testing suites. Selenium is designed in a way to support and encourage automation testing of functional aspects of web based applications and a wide range of browsers and platforms.

 public static WebDriver driver; public static void main(String[] args) { driver = new FirefoxDriver(); // This opens a window String url = "----"; /*driver.findElement(By.id("username")).sendKeys("yashwanth.m"); driver.findElement(By.name("j_password")).sendKeys("yashwanth@123");*/ JavascriptExecutor jse = (JavascriptExecutor) driver; if (jse instanceof WebDriver) { //Launching the browser application jse.executeScript("window.location = \'"+url+"\'"); jse.executeScript("document.getElementById('username').value = \"yash\";"); // Tag having name then driver.findElement(By.xpath(".//input[@name='j_password']")).sendKeys("admin"); //Opend Site and click on some links. then you can apply go(-1)--> back forword(-1)--> front. //Refresheing the web-site. driver.navigate().refresh(); jse.executeScript("window.history.go(0)"); jse.executeScript("window.history.go(-2)"); jse.executeScript("window.history.forward(-2)"); String title = (String)jse.executeScript("return document.title"); System.out.println(" Title Of site : "+title); String domain = (String)jse.executeScript("return document.domain"); System.out.println("Web Site Domain-Name : "+domain); // To get all NodeList[1052] document.querySelectorAll('*'); or document.all jse.executeAsyncScript("document.getElementsByTagName('*')"); String error=(String) jse.executeScript("return window.jsErrors"); System.out.println("Windowerrors : "+error); System.out.println("To Find the input tag position from top"); ArrayList<?> al = (ArrayList<?>) jse.executeScript( "var source = [];"+ "var inputs = document.getElementsByTagName('input');"+ "for(var i = 0; i < inputs.length; i++) { " + " source[i] = inputs[i].offsetParent.offsetTop" + //" inputs[i].type = 'radio';" "}"+ "return source" );//inputs[i].offsetParent.offsetTop inputs[i].type System.out.println("next"); System.out.println("array : "+al); // (CTRL + a) to access keyboard keys. org.openqa.selenium.Keys Keys k = null; String selectAll = Keys.chord(Keys.CONTROL, "a"); WebElement body = driver.findElement(By.tagName("body")); body.sendKeys(selectAll); // Search for text in Site. Gets all ViewSource content and checks their. if (driver.getPageSource().contains("login")) { System.out.println("Text present in Web Site"); } Long clent_height = (Long) jse.executeScript("return document.body.clientHeight"); System.out.println("Client Body Height : "+clent_height); // using selenium we con only execute script but not JS-functions. } driver.quit(); // to close browser } 

To Execute User-Functions, Writing JS in to a file and reading as String and executing it to easily use.

Scanner sc = new Scanner(new FileInputStream(new File("JsFile.txt"))); String js_TxtFile = ""; while (sc.hasNext()) { String[] s = sc.next().split("\r\n"); for (int i = 0; i < s.length; i++) { js_TxtFile += s[i]; js_TxtFile += " "; } } String title = (String) jse.executeScript(js_TxtFile); System.out.println("Title : "+title); 

document.title & document.getElementById() is a property/method available in Browsers.

JsFile.txt

var title = getTitle(); return title; function getTitle() { return document.title; } 
    4

    You can also try clicking by JavaScript:

    WebElement button = driver.findElement(By.id("someid")); JavascriptExecutor jse = (JavascriptExecutor)driver; jse.executeScript("arguments[0].click();", button); 

    Also you can use jquery. In worst cases, for stubborn pages it may be necessary to do clicks by custom EXE application. But try the obvious solutions first.

    1
    • this appears to be pausing for the length of time set by driver.manage().timeouts().implicitlyWait. Is this the case and if so, how to speed it up?CommentedMar 13, 2020 at 1:59
    1

    I didn't see how to add parameters to the method call, it took me a while to find it, so I add it here. How to pass parameters in (to the javascript function), use "arguments[0]" as the parameter place and then set the parameter as input parameter in the executeScript function.

     driver.executeScript("function(arguments[0]);","parameter to send in"); 
      0

      If you want to read text of any element using javascript executor, you can do something like following code:

      WebElement ele = driver.findElement(By.xpath("//div[@class='infaCompositeViewTitle']")); String assets = (String) js.executeScript("return arguments[0].getElementsByTagName('span')[1].textContent;", ele); 

      In this example, I have following HTML fragment and I am reading "156".

      <div class="infaCompositeViewTitle"> <span>All Assets</span> <span>156</span> </div> 
        0

        Following code worked for me:

        import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.springframework.beans.factory.annotation.Autowired; public class SomeClass { @Autowired private WebDriver driver; public void LogInSuperAdmin() { ((JavascriptExecutor) driver).executeScript("console.log('Test test');"); } } 
          -1

          I had a similar situation and solved it like this:

          WebElement webElement = driver.findElement(By.xpath("")); webElement.sendKeys(Keys.TAB); webElement.sendKeys(Keys.ENTER); 
            -3

            You need to run this command in the top-level directory of a Selenium SVN repository checkout.

              Start asking to get answers

              Find the answer to your question by asking.

              Ask question

              Explore related questions

              See similar questions with these tags.