Executing JavaScript using Selenium WebDriver in C#
Executing JavaScript using Selenium WebDriver allows you to interact with the DOM of a web page dynamically. The Java version of this code is:
<code class="java">WebDriver driver; // Assigned elsewhere JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("return document.title");</code>
C# Equivalent
In C#, the corresponding code is:
<code class="csharp">IWebDriver driver; // assume assigned elsewhere IJavaScriptExecutor js = (IJavaScriptExecutor)driver; string title = (string)js.ExecuteScript("return document.title");</code>
Explanation
The C# code uses the IJavaScriptExecutor interface to execute JavaScript on the current page. This interface is implemented by the Selenium WebDriver, which provides you with the ability to execute JavaScript commands within your test scripts.
Additional Information
The complete documentation of the WebDriver API for .NET can be found at this link: https://www.selenium.dev/documentation/webdriver/dotnet/
The above is the detailed content of How to Execute JavaScript Using Selenium WebDriver in C#?. For more information, please follow other related articles on the PHP Chinese website!