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#如何从NoTouchElementException获取方法和选择器_C#_Selenium_Selenium Webdriver - Fatal编程技术网

C#如何从NoTouchElementException获取方法和选择器

C#如何从NoTouchElementException获取方法和选择器,c#,selenium,selenium-webdriver,C#,Selenium,Selenium Webdriver,我正在使用C#.NET 4和Selenium WebDriver 2.44.0.0以及chromeDriver。 当找不到元素时,Selenium抛出错误: no such element (Session info: chrome=38.0.2125.104) (Driver info: chromedriver=2.10.267521,platform=Windows NT 6.1 SP1 x86_64) 但我想知道缺少哪个元素。我看到一些文章说它可以显示这样的细节: OpenQA.Se

我正在使用C#.NET 4和Selenium WebDriver 2.44.0.0以及chromeDriver。 当找不到元素时,Selenium抛出错误:

no such element
(Session info: chrome=38.0.2125.104)
(Driver info: chromedriver=2.10.267521,platform=Windows NT 6.1 SP1 x86_64)

但我想知道缺少哪个元素。我看到一些文章说它可以显示这样的细节:

OpenQA.Selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"hello"}

有人能告诉我如何从NoTouchElementException获取方法和选择器吗


这是我的密码

try
{
     for(int i=0; i<10; i++)
     {
          string className = "items-" + i;
          IWebElement t = Driver.FindElement(By.CssSelector("[class$='" + className + "'] > span"));
          t.Click();
     }
}
catch (NoSuchElementException ex)
{
     Logger.Error("Error: " + ex.Message);
     Debug.WriteLine(ex.Message);
}
试试看
{

有关(int i=0;i的信息,请参阅selenium API文档for.net found。 但是,我认为它不会提供您想要的异常跟踪功能。请打印您正在使用的选择器,它可以帮助您识别引发错误的元素。
定位器的
方法的
返回您想要的内容

据我所知,异常本身并不包含这些信息。但是,处理异常并将其与引发异常的
FindElement()
方法中使用的定位器函数相关联是非常简单的

例如,如果在第5次迭代中找不到元素,下面的代码将生成此消息

错误:没有这样的元素

错误:无法使用function By.CssSelector:[class$='items-4']>span找到元素

By locator;//需要在try块的范围之外声明它
//因此可以在catch块中访问它
尝试
{

对于(int i=0;i谢谢你。我将使用John O.answer的解决方法。谢谢你的回答。但我希望避免修改当前脚本。是否可以配置Selenium以在异常消息中显示更多详细信息?除了消息之外,你还可以选择获取StackTrace。你还需要什么其他信息?我不知道hink Selenium异常有任何配置选项。我在回答中添加了一段,以明确这是一种将信息与异常关联的方式,而不是从异常本身提取信息的方式。您应该真正避免“按异常编码”。异常应该是异常情况—例如硬盘空间不足或网络连接失败。如果您正在编写代码,并且希望代码“正常”运行时出现异常,则说明您做错了。
By locator;     // need to declare this outside the scope of the try block
                // so it can be accessed in the catch block
try
{
     for(int i=0; i<10; i++)
     {
          string className = "items-" + i;
          locatorFunction = By.CssSelector("[class$='" + className + "'] > span")
          IWebElement t = Driver.FindElement(locatorFunction);
          t.Click();
     }
}
catch (NoSuchElementException ex)
{
     Logger.Error("Error: " + ex.Message);
     Logger.Error("Error: Unable to find element using function " + locatorFunction.ToString());
}