在Selenium中避免NoSuchElementException的最佳方法是什么?

我在Selenium WebDriver中使用Java编写了一些测试用例,并在网格(集线器和多个节点)上执行它们。 我注意到一些测试用例由于NoSuchElementException而失败。 什么是避免NoSuchElementException并确保始终找到元素的最佳和最强大的方法?

您永远无法确定是否会找到该元素,实际上这是function测试的目的 – 告诉您页面上是否有任何更改。 但有一点肯定有帮助的是添加等待经常导致NoSuchElementException的元素

 WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id)); 

我完全同意上面的Petr Mensik。 你永远不能说元素是否存在的问题。 你应该清楚地了解它何时发生。 根据我的经验,我应该说它是由于以下原因而发生的:

  • 1)页面仍在渲染中,您已经完成元素搜索并且没有获得元素exception。
  • 2)第二个原因是AJAX还没有返回,你已经获得了NoSuchElementException
  • 3)第三个是最明显的:元素真的不在页面上。

因此,使用一个函数调用来处理所有这三个条件的最强大的IMHO方法是使用fluentWait如Amith003所建议的那样。

所以代码如下:

让ur元素有定位器:

 String elLocXpath= "..blablabla"; WebElement myButton= fluentWait(By.xpath(elLocXpath)); myButton.click(); public WebElement fluentWait(final By locator){ Wait wait = new FluentWait(driver) .withTimeout(30, TimeUnit.SECONDS) .pollingEvery(5, TimeUnit.SECONDS) .ignoring(org.openqa.selenium.NoSuchElementException.class); WebElement foo = wait.until( new Function() { public WebElement apply(WebDriver driver) { return driver.findElement(locator); } } ); return foo; }; 

此外,如果你的目的是强大的代码包装fluentWait()try{} catch{}块。

也别忘了

  public boolean isElementPresent(By selector) { return driver.findElements(selector).size()>0; } 

这也很有用。

因此,如果您想要避免NoElementexception,那么要总结所有提到的内容,只需正确处理它,因为没有人能够确保页面中的元素存在。

希望现在对你来说更清楚了。 问候

你也可以使用FluentWait

每个FluentWait实例定义等待条件的最大时间量,以及检查条件的频率。

此外,用户可以将等待配置为在等待时忽略特定类型的exception,例如在搜索页面上的元素时的NoSuchElementExceptions

 // Waiting 30 seconds for an element to be present on the page, checking // for its presence once every 5 seconds. Wait wait = new FluentWait(driver) .withTimeout(30, SECONDS) .pollingEvery(5, SECONDS) .ignoring(NoSuchElementException.class); WebElement foo = wait.until(new Function() { public WebElement apply(WebDriver driver) { return driver.findElement(By.id("foo")); } }); 

点击这里查看更多信息

 WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds); wait.until(ExpectedConditions.elementToBeClickable(By.id)); 

elementToBeClickable等待元素的启用可见

 public WebElement fluientWaitforElement(WebElement element, int timoutSec, int pollingSec) { FluentWait fWait = new FluentWait(driver).withTimeout(timoutSec, TimeUnit.SECONDS) .pollingEvery(pollingSec, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class, TimeoutException.class); for (int i = 0; i < 2; i++) { try { //fWait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//*[@id='reportmanager-wrapper']/div[1]/div[2]/ul/li/span[3]/i[@data-original--title='We are processing through trillions of data events, this insight may take more than 15 minutes to complete.']"))); fWait.until(ExpectedConditions.visibilityOf(element)); fWait.until(ExpectedConditions.elementToBeClickable(element)); } catch (Exception e) { System.out.println("Element Not found trying again - " + element.toString().substring(70)); e.printStackTrace(); } } return element; } 

我通常在main函数中使用这一行

 public static void main(String[] args) throws ParseException { driver= new ChromeDriver(); driver.manage().window().maximize(); **driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);** 

希望这可以帮助。

我们可以应用以下代码来删除此exception情况

  1. 通过应用WebDriverWait,webdriver对象等待元素的特定时间(以秒为单位)以获得其可见性。

      WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.visibilityOf(link)); 
  2. 我们可以通过Generic方法中的try-catch块来处理NoSuchElementException

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

http://selenium-code.blogspot.in/2017/08/selenium-exception-nosuchelementexcepti.html