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:
WebDriver driver; // Assigned elsewhere
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("return document.title");
C# Equivalent
In C#, the corresponding code is:
IWebDriver driver; // assume assigned elsewhere
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
string title = (string)js.ExecuteScript("return document.title");
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/
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3