【问题标题】:Unable to cast object of type 'OpenQA.Selenium.Remote.RemoteWebElement' to type 'System.Collections.Generic.Di ctionary`2[System.String,System.Object]无法将“OpenQA.Selenium.Remote.RemoteWebElement”类型的对象转换为“System.Collections.Generic.Dictionary”2 [System.String,System.Object]
【发布时间】:2012-06-13 21:01:01
【问题描述】:

我正在尝试使用 Web Timing API 和 Selenium 2 webdrivers 和 C# 来捕获我的网页加载的各种事件(以捕获性能)所花费的时间

基本上这个想法(最初来自 Mozilla 团队的开发人员)来自 Dean Hume 的博客文章...... http://deanhume.com/Home/BlogPost/measuring-web-page-performance-with-selenium-2-and-the-web-timings-api/56

我无耻地复制了扩展类并写了几个方法来获取我想要的格式的数字..

public static class Extensions
{
    public static Dictionary<string, object> WebTimings(this IWebDriver driver)
    {
        const string scriptToExecute =
            "var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {}; var timings = performance.timing || {}; return timings;";

        var webTiming = (Dictionary<string, object>)((IJavaScriptExecutor)driver)
            .ExecuteScript(scriptToExecute);

        return webTiming;
    }
}

我的方法会给我时差...

        //InternetExplorerOptions options = new InternetExplorerOptions();
        //options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
        //options.UseInternalServer = true;
        //IWebDriver _driver = new InternetExplorerDriver(options);

        IWebDriver _driver = new FirefoxDriver();

        //IWebDriver _driver = new ChromeDriver();

        Program p = new Program();

        _driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));
        _driver.Navigate().GoToUrl("http://www.google.com");

        Dictionary<string, object> webTimings = _driver.WebTimings();
        Dictionary<string, Int64> timeinSec = new Dictionary<string, Int64>();

        timeinSec.Add("ConnectTime", p.GetTimeDiff(webTimings["connectEnd"], webTimings["connectStart"]));

我在使用 InternetExplorerDriver(options) 时遇到了这个异常。但相同的代码适用于 Firefox 和 chrome 驱动程序。

IE 总是让人头疼,而且事实证明它继续如此 ** 我不明白我还能如何投射 .ExecuteScript(scriptToExecute); 返回的内容...?

在这里感谢任何输入..

Unhandled Exception: System.InvalidCastException: Unable to cast object of type
'OpenQA.Selenium.Remote.RemoteWebElement' to type 'System.Collections.Generic.Di
ctionary`2[System.String,System.Object]'.
   at Selenium.Examples.Performance.Extensions.WebTimings(IWebDriver driver) in
C:\Users\......\Extensions.cs:line 13
   at PerfCaptureSample.Program.Main(String[] args) in C:\.........\Program.cs:line 52

【问题讨论】:

  • 不要将对象转换为字典。将它分配给一个 var 然后检查这个变量的值是什么
  • 抱歉,我没有做足够的调查,我的回答是垃圾,所以我删除了它。我尝试在 Java 中执行相同的脚本并遇到同样的问题,即 IE 没有返回它应该返回的内容。

标签: c# web selenium-webdriver timing


【解决方案1】:

虽然老得令人难以置信,但我遇到了同样的问题。我发现您可以使用 toJSON 转换对象并正确返回。工作代码如下:

return timings.toJSON();

最终代码为:

public static class Extensions
{
    public static Dictionary<string, object> WebTimings(this IWebDriver driver)
    {
        const string scriptToExecute =
            "var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {}; var timings = performance.timing || {}; return timings.toJSON();";

        var webTiming = (Dictionary<string, object>)((IJavaScriptExecutor)driver)
        .ExecuteScript(scriptToExecute);

        return webTiming;
    }
}

【讨论】:

    猜你喜欢
    • 2020-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-28
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    相关资源
    最近更新 更多