NoSuchElementException

NoSuchElementException

本文介绍了在 Selenium 中避免 NoSuchElementException 的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Java 在 Selenium WebDriver 中编写了一些测试用例,并在网格(集线器和多个节点)上执行它们.我注意到一些测试用例由于 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 的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-27 07:35