Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Selenium IWebElement到PhantomJSWebElement_C#_Selenium_Xpath_Ghostdriver - Fatal编程技术网

C# Selenium IWebElement到PhantomJSWebElement

C# Selenium IWebElement到PhantomJSWebElement,c#,selenium,xpath,ghostdriver,C#,Selenium,Xpath,Ghostdriver,我在C项目中使用Ghost驱动程序PhantomJS。我有个问题。 Selenium有PhantomJSWebElement和PhantomJSDriver。 我正在创建PhantomJSDriver PhantomJSDriverService service = PhantomJSDriverService.CreateDefaultService(); service.IgnoreSslErrors = true; service.LoadImages = false; service.S

我在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;
   }
}

也不能抛出铸造异常。所以问题是,如何通过PhantomJSDriver获取PhantomJSWebElement在查找时只返回IWebElement或它们的集合。

通常,在使用.NET绑定时,不应使用浏览器特定的类。相反,您应该按照预期的接口进行编码。这允许您根据需要在不同的实现中进行替换。您的代码如下所示:

PhantomJSDriverService service = PhantomJSDriverService.CreateDefaultService();
service.IgnoreSslErrors = true;
service.LoadImages = false;

// Not sure I'd use Start here. The constructor will start the service
// for you.
service.Start();

// Use the IWebDriver interface. There's no real advantage to using
// the PhantomJSDriver class.
IWebDriver ghostDriver = new PhantomJSDriver(service);

// ...

List<string> retVal = new List<string>();
var aElements = ghostDriver.FindElements(By.XPath("//div[@id='menu']//a[@href]"));

// Use the IWebElement interface here. The concrete PhantomJSWebElement
// implementation gives you no advantages over coding to the interface.
foreach(IWebElement link in aElements)
{
    try
    {
        retVal.Add(link.GetAttribute("href"));
    }
    catch (Exception)
    {
        continue;
    }
}

您还应该注意,类文档很可能是不正确的。了解语言绑定的源代码后,PhantomJSWebElement类实际上从未在任何地方实例化。我相信您实际上从FindElements调用中得到的是RemoteWebElements,因此尝试将它们从继承层次结构中向下转换到更具体的子类是注定要失败的。

如果不发布确切的异常文本,这个问题很难回答。另外,您是否已经使用调试器处理了此异常?链接是什么类型的?我有俄文版本的VS。文本是:БааааааааааааааабббъааааOpenQA.Selenium.Remote.Remote WebElementа。PhantomJSWebElement继承自RemoteWebElement。您是否尝试过这样的强制转换:PhantomJSWebElement el=RemoteWebElementlink;相反?哪里没有WebElement。PhantomJSWebElement继承自RemoteWebElementDouble强制转换,就像PhantomJSWebElementRemoteWebElement具有相同的异常一样。
PhantomJSDriverService service = PhantomJSDriverService.CreateDefaultService();
service.IgnoreSslErrors = true;
service.LoadImages = false;

// Not sure I'd use Start here. The constructor will start the service
// for you.
service.Start();

// Use the IWebDriver interface. There's no real advantage to using
// the PhantomJSDriver class.
IWebDriver ghostDriver = new PhantomJSDriver(service);

// ...

List<string> retVal = new List<string>();
var aElements = ghostDriver.FindElements(By.XPath("//div[@id='menu']//a[@href]"));

// Use the IWebElement interface here. The concrete PhantomJSWebElement
// implementation gives you no advantages over coding to the interface.
foreach(IWebElement link in aElements)
{
    try
    {
        retVal.Add(link.GetAttribute("href"));
    }
    catch (Exception)
    {
        continue;
    }
}