hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Javascript Executor in Selenium


Javascript Executor is a Selenium Interface which directly lets you Interact with HTML DOM of the webpage. JavascriptExecutor provides a way to automate a user interaction even when page is not essentially loaded completely or elements are placed in a way that the direct interaction is blocked.

Selenium supports javaScriptExecutor. There is no need for an extra plugin or add-on. You just need to import (org.openqa.selenium.JavascriptExecutor) in the script as to use JavaScriptExecutor.


The basic syntax for JavascriptExecutor is given below:


Syntax:

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript(Script,Arguments);

Script – This is the JavaScript that needs to execute.

Arguments – It is the arguments to the script. It’s optional.



Why do we need JavaScriptExecutor?

JavascriptExecutor in Selenium is very crucial when you are having some problems while using Selenium built-in methods. Sometimes we cannot handle some conditions or problems with Webdriver, web controls don’t react well against selenium commands. In these kinds of situations, we use the JavascriptExecutor Selenium interface.

It is useful for custom synchronizations, hide or show the web elements, change values, test flash/HTML5, and so on.

JavascriptExecutor Interface has two abstract methods to run javascript on the selected window or current page which are :

· executeScript()

· executeAsyncScript()


executeScript method – This method executes the test script in the context of the currently selected window or frame in Selenium. We can also pass complicated arguments to it. If the script has a return statement, the following values are returned:

· For an HTML element, the method returns a WebElement.

· For a decimal, the method returns Long.

· For a non-decimal number, the method returns Long.

· For a Boolean, the method returns Boolean.

· For other cases, the method returns a String.

· For all other cases, a String is returned.

· For an array, return a List<Object> with each object following the rules above. We support nested lists.

· Unless the value is null or there is no return value, in which null is returned.



executeAsyncScript()method

With Asynchronous script, your page renders more quickly Instead of forcing users to wait for a script to download before the page renders.

· This function will execute an asynchronous piece of JavaScript in the context of the currently selected frame or window in Selenium.

· The JS so executed is single-threaded with a various callback function which runs synchronously.

· Using the executeAsyncScript, helps to improve the performance of your test.

· It allows writing test more like a normal coding.

· execAsync does not block action being performed by the Selenium browser.

· It will send a callback to the server-side Testing suite once the script is done.It means everything inside the script will be executed by the browser and not the server.

JavaScriptExecutor examples

1.To click a button without using click() method:

js.executeScript(“arguments[0].click();”, WebElement);


2. To type Text without using sendKeys() method:

js.executeScript(“document.getElementById(‘some id’).value=’someValue’;”);


3. To select checkbox:

js.executeScript(“document.getElementById(‘enter element id’).checked=false;”);


4. To generate Alert Pop window:

js.executeScript(“alert(‘Hello World’);”);


5. To refresh browser window:

js.executeScript(“history.go(0)”);


6. To get innertext of the entire webpage:

String sText = js.executeScript(“return document.documentElement.innerText;”).toString();


7. To get the Title of a webpage:

String sText = js.executeScript(“return document.title;”).toString();


8. To get the domain:

String sText = js.executeScript(“return document.domain;”).toString();


9. To get the URL of a webpage:

String sText = js.executeScript(“return document.URL;”).toString();


10. To perform Scroll operations:

//Vertical scroll down by 500 pixels:

js.executeScript(“window.scrollBy(0,500)”);


//scroll down till the bottom of the webpage:

js.executeScript(“window.scrollBy(0,document.body.scrollHeight)”);


11. To click on a SubMenu which is only visible on mouse hover on Menu:

js.executeScript(“$(‘ul.menus.menu-secondary.sf-js-enabled.sub-menu li’).hover()”);


12. To navigate to different page:

js.executeScript(“window.location = ‘pageUrl’”);

13. To find hidden element:

js.executeScript(“arguments[0].click();”, element);


We can use JavaScript Executors in most of our webscraping projects to eliminate Synchronization errors and increase performance.


executeAsyncscript example:




executescript example:





I hope this blog was useful to the Reader. Thank you.










144 views0 comments

Recent Posts

See All

XPath