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

How to Execute JavaScript Using Selenium WebDriver in C#?

Susan Sarandon
Release: 2024-10-19 06:04:02
Original
1087 people have browsed it

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>
Copy after login

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>
Copy after login

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!

source:php
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template