我在 C# 中将 Selenium Web 驱动程序与 Visual Studios 2010 一起使用。我正在使用 jQuery 过滤掉 div 列表并使用 Selenium 双击它们。但是,无论我做什么,我似乎都无法摆脱 InvalidCastException。
这是我写的代码:
IWebDriver m_driver = new ChromeDriver();
IJavaScriptExecutor js = m_driver as IJavaScriptExecutor;
string jsQuery = [insert some query here that returns list of divs];
object result = js.ExecuteScript(jsQuery);
System.Collections.ObjectModel.ReadOnlyCollection<IWebElement> list = (System.Collections.ObjectModel.ReadOnlyCollection<IWebElement>)result;
结果确实返回了一个 webelements 列表,但由于某种原因,有时上面的代码运行良好并且强制转换,而其他时候它不在 ChromeDriver 上。当它不起作用时,提供的最后一行代码将失败,并显示以下内容:
"Unable to cast object of type 'System.Collections.ObjectModel.ReadOnlyCollection`1[System.Object]' to type 'System.Collections.ObjectModel.ReadOnlyCollection`1[OpenQA.Selenium.IWebElement]'."
在 InternetExplorerDriver 上,它几乎总是失败,原因如下:
Unable to cast object of type 'OpenQA.Selenium.Remote.RemoteWebElement' to type 'System.Collections.ObjectModel.ReadOnlyCollection`1[OpenQA.Selenium.IWebElement]'.
我已经尝试将它转换为 IE 的 RemoteWebElement,但这也不起作用,因为它看到的是 RemoteWebElement 而不是 RemoteWebElements 的列表,因此我以后无法枚举它们。
关于为什么的任何线索?