问题描述
我在Selenium WebDriver中使用Java编写了一些测试用例,并在网格(集线器和多个节点)上执行它们。我注意到一些测试用例由于 NoSuchElementException
而失败。什么是避免 NoSuchElementException
并确保始终找到元素的最佳且最有效的方法?
I have written few test cases in Selenium WebDriver using Java and execute them on grid (hub and multiple nodes). I have noticed that a few test cases fail due to NoSuchElementException
. What is the best and robust way to avoid NoSuchElementException
and ensure the element is always found?
推荐答案
您永远无法确定是否会找到该元素,实际上这是功能测试的目的 - 告诉您页面上是否有任何更改。但有一点肯定有帮助的是添加等待通常导致 NoSuchElementException
的元素,比如
You can never be sure that element will be found, actually this is purpose of functional tests - to tell you if anything changed on your page. But one thing which definitely helps is to add waits for the elements which are often causing NoSuchElementException
like
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));
这篇关于在Selenium中避免NoSuchElementException的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!