简体   繁体   English

无法在 C# 中使用 Selenium WebDriver 执行 JavaScript 命令

[英]Can not Execute JavaScript command using Selenium WebDriver in C#

I can not execute js command using selenium web driver.我无法使用 selenium Web 驱动程序执行 js 命令。 for example, I used 3 ways for this URL but all of them return null.例如,我对这个 URL 使用了 3 种方式,但它们都返回 null。

                    web.FindElementByJs("StackExchange.init.length");

                    IJavaScriptExecutor js = (IJavaScriptExecutor)web;
                    string title = (string)js.ExecuteScript("StackExchange.init.length");

                    web.ExecuteJavaScript("StackExchange.init.length");

I get it!我得到它! For an object, I should use 'window'.对于对象,我应该使用“窗口”。

IJavaScriptExecutor js = (IJavaScriptExecutor)web;
string title = (string)js.ExecuteScript("return window.StackExchange.init.length");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM