在 C# 中使用 Selenium WebDriver 执行 JavaScript [英] Execute JavaScript using Selenium WebDriver in C#

查看:50
本文介绍了在 C# 中使用 Selenium WebDriver 执行 JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是如何实现的?这里它说java版本是:

How is this achieved? Here it says the java version is:

WebDriver driver; // Assigned elsewhere
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("return document.title");

但我找不到执行此操作的 C# 代码.

But I can't find the C# code to do this.

推荐答案

.NET 语言绑定中的对象、方法和属性名称与 Java 绑定中的不完全对应.该项目的原则之一是,对于那些使用该语言进行编码的人来说,每种语言绑定都应该感觉自然".在 C# 中,执行 JavaScript 所需的代码如下

The object, method, and property names in the .NET language bindings do not exactly correspond to those in the Java bindings. One of the principles of the project is that each language binding should "feel natural" to those comfortable coding in that language. In C#, the code you'd want for executing JavaScript is as follows

IWebDriver driver; // assume assigned elsewhere
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
string title = (string)js.ExecuteScript("return document.title");

请注意,用于 .NET 的 WebDriver API 的完整文档可以在 此链接.

Note that the complete documentation of the WebDriver API for .NET can be found at this link.

这篇关于在 C# 中使用 Selenium WebDriver 执行 JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆