6/23/2020 Selenium Execute Javascript
Well the answer is simple, as we already saw in the post of highlighting an element using Selenium, we used IJavaScriptExecutor which is an Interface to execute a Javascript or JQuery, this interface has an method called ExecuteScript, which will does the execution of script and return the value.
What is JavaScriptExecutor?
JavaScriptExecutor is an Interface that helps to execute JavaScript through Selenium Webdriver. JavaScriptExecutor provides two methods 'executescript' & 'executeAsyncScript' to run javascript on the selected window or current page.
In this tutorial, you will learn -
Why do we need JavaScriptExecutor?
In Selenium Webdriver, locators like XPath, CSS, etc. are used to identify and perform operations on a web page.
In case, these locators do not work you can use JavaScriptExecutor. You can use JavaScriptExecutor to perform an desired operation on a web element.
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.
JavaScriptExecutor Methods
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.
This method executes JavaScript in the context of the currently selected frame or window in Selenium. The script used in this method runs in the body of an anonymous function (a function without a name). We can also pass complicated arguments to it.
The script can return values. Data types returned are
Tomtom driver update. The basic syntax for JavascriptExecutor is given below:
Syntax:
Example of executeAsyncScript
Using the executeAsyncScript, helps to improve the performance of your test. It allows writing test more like a normal coding.
The execSync blocks further actions being performed by the Selenium browser but execAsync does not block action. 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.
Example 1: Performing a sleep in the browser under test.
In this scenario, we will use 'Guru99' demo site to illustrate executeAsyncScript. In this example, you will
Step 1) Capture the start time before waiting for 5 seconds ( 5000 milliseconds) by using executeAsyncScript() method.
Step 2) Then, use executeAsyncScript() to wait 5 seconds.
Step 3) Then, get the current time.
Step 4) Subtract (current time – start time) = passed time.
Step 5) Verify the output it should display more than 5000 milliseconds
Output: Successfully displayed the passed time more than 5 seconds(5000 miliseconds) as shown below:
Example of executeScript
For executeScript, we will see three different example one by one.
1) Example: Click a button to login and generate Alert window using JavaScriptExecutor.
In this scenario, we will use 'Guru99' demo site to illustrate JavaScriptExecutor. In this example,
Output: When the code is executed successfully. You will observe
2) Example: Capture Scrape Data and Navigate to different pages using JavaScriptExecutor.
Execute the below selenium script. In this example,
Output: When above code is executed successfully, it will it will fetch the details of the site and navigate to different page as shown below.
3) Example: Scroll Downusing JavaScriptExecutor.
Execute the below selenium script. In this example,
![]()
Output: When above code is executed, it will scroll down by 600 pixels (see image below).
Summary:
![]()
JavaScriptExecutor is used when Selenium Webdriver fails to click on any element due to some issue.
Active8 months ago
How is this achieved? Here it says the java version is:
But I can't find the C# code to do this.
Ripon Al Wasim
27.8k3232 gold badges131131 silver badges154154 bronze badges
JontyMCJontyMC
Selenium Execute Javascript C#
2,76944 gold badges2626 silver badges3737 bronze badges
7 Answers
The object, method, and property names in the .NET language bindings do not exactly correspond to those in the Java bindings. One of the principles of the project is that each language binding should 'feel natural' to those comfortable coding in that language. In C#, the code you'd want for executing JavaScript is as follows
Note that the complete documentation of the WebDriver API for .NET can be found at this link.
ESV
Selenium Execute Javascript
6,47333 gold badges3333 silver badges2929 bronze badges
JimEvansJimEvans
23.2k55 gold badges6969 silver badges9494 bronze badges
I prefer to use an extension method to get the scripts object:
Used as this:
tanascius
44k1515 gold badges104104 silver badges127127 bronze badges
Selenium Execute Javascript RubyMorten ChristiansenMorten ChristiansenSelenium Execute Javascript Onclick
12.6k1919 gold badges5656 silver badges8888 bronze badges
How about a slightly simplified version of @Morten Christiansen's nice extension method idea:
or maybe the generic version:
agentnegaagentnega
2,59511 gold badge1919 silver badges3030 bronze badges
the nuget package
Selenium.Support already contains an extension method to help with this. Once it is included, one liner to executer script
or
harishrharishr
14k55 gold badges5757 silver badges9595 bronze badges
perryzhengperryzheng
akhilesh gulatiakhilesh gulati
Selenium Execute Javascript File
Mo D GenesisMo D Genesis
Python Selenium Execute JavascriptNot the answer you're looking for? Browse other questions tagged javascriptc#selenium-webdriverwebdriver or ask your own question.Comments are closed.
|