Java Selenium Webdriver:处理 NoSuchElementException 的最佳实践

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22780964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:49:49  来源:igfitidea点击:

Selenium Webdriver: best practice to handle a NoSuchElementException

javaselenium-webdriverjunit

提问by Steerpike

After much searching and reading, I'm still unclear as to the best way to handle a failed assertion using Webdriver. I would have thought this was a common and core piece of functionality. All I want to do is:

经过大量搜索和阅读,我仍然不清楚使用 Webdriver 处理失败断言的最佳方法。我会认为这是一个常见的核心功能。我想做的就是:

  • look for an element
  • if present - tell me
  • if not present - tell me
  • 寻找一个元素
  • 如果存在 - 告诉我
  • 如果不存在 - 告诉我

I want to present the results for a non technical audience, so having it throw 'NoSuchElementExceptions' with a full stack trace is not helpful. I simply want a nice message.

我想向非技术受众展示结果,因此让它抛出带有完整堆栈跟踪的“NoSuchElementExceptions”并没有帮助。我只是想要一个好消息。

My test:

我的测试:

@Test
public void isMyElementPresent(){
  //  WebElement myElement driver.findElement(By.cssSelector("#myElement"));
    if(driver.findElement(By.cssSelector("#myElement"))!=null){
        System.out.println("My element was found on the page");
    }else{
            System.out.println("My Element was not found on the page");
        }
    }

I still get a NoSuchElementException thrown when I force a fail. Do I need a try/catch as well? Can I incorporate Junit assertions and/or Hamcrest to generate a more meaningful message without the need for a System.out.println?

当我强制失败时,我仍然会抛出 NoSuchElementException。我也需要尝试/捕获吗?我可以在不需要 System.out.println 的情况下合并 Junit 断言和/或 Hamcrest 来生成更有意义的消息吗?

采纳答案by Vish

I have encountered similar situations. According to the Javadocfor the findElementand findElementsAPIs, it appears that the findElementbehavior is by design. You should use findElementsto check for non-present elements.

我也遇到过类似的情况。按照的JavadocfindElementfindElementsAPI的,看来该findElement行为是由设计。您应该使用findElements来检查不存在的元素。

Since in your case, there's a chance that the WebElement is not present, you should use findElementsinstead.

由于在您的情况下,有可能 WebElement 不存在,您应该findElements改用。

I'd use this as follows.

我会按如下方式使用它。

List<WebElement> elems = driver.findElements(By.cssSelector("#myElement"));
if (elems.size == 0) {
        System.out.println("My element was not found on the page");
} else
        System.out.println("My element was found on the page");
}

回答by xyz

you can do something to check if element exists

你可以做一些事情来检查元素是否存在

  public boolean isElementExists(By by) {
    boolean isExists = true;
    try {
        driver.findElement(by);
    } catch (NoSuchElementException e) {
        isExists = false;
    }
    return isExists;
}

回答by Nic

What about using an xPath inside of a try-catch, passing the elementype, attribute and text as follows?

如何在 try-catch 中使用 xPath,按如下方式传递元素类型、属性和文本?

try {
    driver.FindElement(
        By.XPath(string.Format("//{0}[contains(@{1}, '{2}')]", 
        strElemType, strAttribute, strText)));
    return true;
}
catch (Exception) {
    return false;
}