【发布时间】:2014-01-14 19:37:12
【问题描述】:
我在我的 C# 项目中使用 Ghost 驱动程序 (PhantomJS)。我有个问题。 Selenium 有 PhantomJSWebElement 和 PhantomJSDriver。 我正在创建 PhantomJSDriver
PhantomJSDriverService service = PhantomJSDriverService.CreateDefaultService();
service.IgnoreSslErrors = true;
service.LoadImages = false;
service.Start();
PhantomJSDriver ghostDriver = new PhantomJSDriver(service);
然后尝试通过 xpath 查找元素
List<string> retVal = new List<string>();
var aElements = ghostDriver.FindElementsByXPath("//div[@id='menu']//a[@href]");
foreach(PhantomJSWebElement link in aElements)
{
try
{
retVal.Add(link.GetAttribute("href"));
}
catch (Exception)
{
continue;
}
}
所以我在将IWebElemet 转换为PhantomJSWebElement 时出错。
PhantomJSWebElement el = (PhantomJSWebElement)link;
也不起作用(抛出转换异常)。那么问题来了,如何通过 PhantomJSDriver 获取 PhantomJSWebElement 在查找时只返回 IWebElement(或它们的集合)。
【问题讨论】:
-
如果不发布确切的异常文本,这个问题很难回答。另外,你有没有用你的调试器处理过这个异常?
type是什么link? -
我在 VS 上有俄语版。文本为:Не удалось привести тип объекта "OpenQA.Selenium.Remote.RemoteWebElement" к типу "OpenQA.Selenium.PhantomJS.PhantomJSWebElement"。它在 aElements 中的 PhantomJSWebElement 链接处抛出
-
我认为
link的类型是WebElement。PhantomJSWebElement继承自RemoteWebElement。您是否尝试过像这样投射:PhantomJSWebElement el = (RemoteWebElement)link;? -
哪里没有WebElement。 PhantomJSWebElement 继承自 RemoteWebElement
-
双重转换像 (PhantomJSWebElement)(RemoteWebElement) 有同样的例外。
标签: c# selenium xpath ghostdriver