Execute JavaScript using Selenium WebDriver in C#
Solution 1
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
IWebDriver driver; // assume assigned elsewhere
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
string title = (string)js.ExecuteScript("return document.title");
Note that the complete documentation of the WebDriver API for .NET can be found at this link.
Solution 2
I prefer to use an extension method to get the scripts object:
public static IJavaScriptExecutor Scripts(this IWebDriver driver)
{
return (IJavaScriptExecutor)driver;
}
Used as this:
driver.Scripts().ExecuteScript("some script");
Solution 3
the nuget package Selenium.Support
already contains an extension method to help with this. Once it is included, one liner to executer script
Driver.ExecuteJavaScript("console.clear()");
or
string result = Driver.ExecuteJavaScript<string>("console.clear()");
Solution 4
How about a slightly simplified version of @Morten Christiansen's nice extension method idea:
public static object Execute(this IWebDriver driver, string script)
{
return ((IJavaScriptExecutor)driver).ExecuteScript(script);
}
// usage
var title = (string)driver.Execute("return document.title");
or maybe the generic version:
public static T Execute<T>(this IWebDriver driver, string script)
{
return (T)((IJavaScriptExecutor)driver).ExecuteScript(script);
}
// usage
var title = driver.Execute<string>("return document.title");
Solution 5
You could also do:
public static IWebElement FindElementByJs(this IWebDriver driver, string jsCommand)
{
return (IWebElement)((IJavaScriptExecutor)driver).ExecuteScript(jsCommand);
}
public static IWebElement FindElementByJsWithWait(this IWebDriver driver, string jsCommand, int timeoutInSeconds)
{
if (timeoutInSeconds > 0)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
wait.Until(d => d.FindElementByJs(jsCommand));
}
return driver.FindElementByJs(jsCommand);
}
public static IWebElement FindElementByJsWithWait(this IWebDriver driver, string jsCommand)
{
return FindElementByJsWithWait(driver, jsCommand, s_PageWaitSeconds);
}
Related videos on Youtube
JontyMC
Updated on July 08, 2022Comments
-
JontyMC almost 2 years
How is this achieved? Here it says the java version is:
WebDriver driver; // Assigned elsewhere JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("return document.title");
But I can't find the C# code to do this.
-
joelsand about 11 yearsNice idea. For others reading this, your javascript code should return a DOM element.
-
rahoolm about 9 yearsCan this be used to get WebElement and bool as well?
-
WheretheresaWill over 8 yearsNOTE the capitalisation in the word JavaScript. This caught me out.
-
Ross Patterson almost 8 yearsJim's answer is about as authoritative as you can get. Check out the commit history on, well, every Selenium .NET file :-)
-
Almett almost 8 yearsCould you please tell me how to click a button in C#? Answers like
arguments[0].click();
is not working for me? I have no exception when i execute my code, but it doesn't click the button. -
jibbs over 7 yearsI didn't even consider this as possible. This is huge as I can now create a method using javascript to return nextSibling.
-
Jeffrey LeCours over 6 yearsThis is a nice, modern solution. The extension method adds validation that the driver implements
IJavaScriptExecutor
and gives a better exception message if the return type is null when it shouldn't be or can't be cast to the desired return type. -
anatol over 5 yearswhat is the
Driver
? VS can't recognize that -
Kellen Stuart almost 5 yearsI find it awkward that I have to cast the driver. Why is
ExecuteJavascript
not just a method on thedriver
? -
Kellen Stuart almost 5 yearsSelenium should have this by default
-
JimEvans almost 5 yearsThat's a leftover from a time when not every driver implementation supported executing arbitrary JavaScript. The .NET bindings, like the Java ones use role-based interfaces to model functionality that may be supported by one driver, but not all. In the Support assembly (
WebDriver.Support.dll
, available via NuGet in theSelenium.Support
package), there's an extension method that handles the casting for you and makes it look like the driver has anExecuteJavaScript
method.