无法将类型为“OpenQA.Selenium.Remote.RemoteWebElement”的对象转换为类型“Syst
本文关键字:类型 对象 Syst RemoteWebElement 转换 Selenium OpenQA Remote | 更新日期: 2023-09-27 18:37:10
我正在尝试使用 Web 计时 API 和 Selenium 2 网络驱动程序和 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(选项)时,我遇到了这个异常。但是相同的代码适用于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
虽然老得不可思议,但我遇到了同样的问题。我发现您可以使用 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;
}
}