Home  >  Article  >  Web Front-end  >  How to Execute JavaScript Using Selenium WebDriver in C#?

How to Execute JavaScript Using Selenium WebDriver in C#?

Susan Sarandon
Susan SarandonOriginal
2024-10-19 06:04:02960browse

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:

<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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn