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?

kenorb
  • 155,785
  • 88
  • 678
  • 743
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176

8 Answers8

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';" +
             "}" );
    
Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
  • This is helpful. if (driver instanceof JavascriptExecutor) { ((JavascriptExecutor)driver)executeScript("yourScript();"); } "yourScript()" Is it Javascript function? If so, where can I write JS code? – Ripon Al Wasim Jul 17 '12 at 04:50
  • @RiponAlWasim That's the nice thing! You can write any JS code into the `executeScript()` function. I added some examples. – Petr Janeček Jul 17 '12 at 09:26
  • I have the JS code as: I want to use executeScript() as: JavascriptExecutor js = (JavascriptExecutor)driver; js.executeScript("showAlert()"); What is the exact/proper syntax of writing the above JS code in Java code using WebDriver? – Ripon Al Wasim Jul 17 '12 at 11:24
  • 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()");` – Petr Janeček Jul 17 '12 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? – Ripon Al Wasim Jul 18 '12 at 04:08
  • @RiponAlWasim You can do `js.executeScript("window.showAlert = function() { alert('success'); };");` and then, anytime later, the call: `js.executeScript("showAlert()");` – Petr Janeček Jul 18 '12 at 15:10
  • 3
    nice. Again thanks. It is working very well & I have got the exact solution – Ripon Al Wasim Jul 19 '12 at 03:24
  • So the first argument in the executeScript is the script itself that is method's body and the second argument is the method's arguments, right? – Code Enthusiastic Jul 05 '13 at 08:13
  • 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). – Petr Janeček Jul 06 '13 at 16:12
  • @Slanec can I ask when does selenium run the javascript? like is it after the the last function run of the page's javascript or...? – rrw Mar 17 '16 at 09:27
  • @richmondwang It runs it as soon as it can. Whether you tell Selenium to click an element or to run a piece of Javascript doesn't matter, both would be run the same time. Does that answer your question? What are you trying to achieve? What are you asking about? – Petr Janeček Mar 20 '16 at 18:18
  • @Slanec so it runs asynchronously with the page's javascripts? just wanted to know. – rrw Mar 21 '16 at 07:27
  • @PetrJaneček what if I need to include an external script from CDN url inside executeScript? How do I do that? – Srk95 Jul 13 '21 at 06:31
  • 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: https://sqa.stackexchange.com/questions/2921/webdriver-can-i-inject-a-jquery-script-for-a-page-that-isnt-using-jquery – Petr Janeček Jul 13 '21 at 09: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;
}
Yash
  • 9,250
  • 2
  • 69
  • 74
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.

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Lukasz
  • 368
  • 4
  • 9
  • 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? – kraftydevil Mar 13 '20 at 01: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");
David Marciel
  • 865
  • 1
  • 12
  • 29
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>
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
muhdchoaib
  • 33
  • 5
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');");
     }
}
Habib Mammadov
  • 311
  • 4
  • 4
-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);
DavidHyogo
  • 2,838
  • 4
  • 31
  • 48
-3

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

Daniel Wagner
  • 2,717
  • 1
  • 21
  • 14