What is the best way to avoid NoSuchElementExcepti

2019-01-09 03:28发布

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?

7条回答
爷、活的狠高调
2楼-- · 2019-01-09 04:19

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>));
查看更多
登录 后发表回答