"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Execute JavaScript Using Selenium WebDriver in C#?

How to Execute JavaScript Using Selenium WebDriver in C#?

Published on 2024-11-04
Browse:682

How to Execute JavaScript Using Selenium WebDriver in C#?

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/

Release Statement This article is reprinted at: 1729288876 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

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