java - 找不到此类元素错误

标签 java selenium-webdriver

无论我使用什么搜索类型,我总是收到“找不到这样的元素”错误。为什么会出现这种情况?

public void CorrectPIN() throws InterruptedException{
    driver.findElement(By.id("identifier")).sendKeys("abhisingh1313@mailinator.com");
    driver.findElement(By.id("button")).click();
    Thread.sleep(5000);
     do {
        Thread.sleep(100);
    } while (driver.findElement(By.id("pin")).isDisplayed());
}

...................... 无论我使用什么搜索机制,我都无法在 driver.findElement(By.id("pin")).isDisplayed()) 上找到元素。我什至尝试过xpath。 基本上我希望网络驱动程序等到屏幕上出现一个元素并且它出现了,但即使这样我也不知道为什么它会给出错误无法定位元素错误。

最佳答案

如果我正确理解了您的问题,在检查while()内的条件时执行了所有步骤之后循环即

driver.findElement(By.id("pin")).isDisplayed();

您正在看到 NoSuchElementException

Java 文档明确提到 NoSuchElementException WebDriver.findElement(By by) 抛出 WebElement.findElement(By by) 符合您的情况。

原因

查看 NoSuchElementException 的理由有很多。 。其中一些如下:

  • Locator Strategy 您已经使用过,即 id可能无法识别确切的元素。
  • 该元素可能不在 Viewport 范围内.
  • 当您的脚本搜索该元素时,该元素可能尚未在 HTML DOM 中呈现。

解决方案

针对上述原因的解决方案是:

<小时/>

更新

您正在看到NoSuchElementException在下面一行:

while (driver.findElement(By.id("pin")).isDisplayed());

从您的代码块来看,您为什么要检查 driver.findElement(By.id("pin")).isDisplayed() 尚无定论。 while loop 。一旦你这样做driver.findElement(By.id("button")).click(); 您将被重定向到带有新 HTML DOM 的新页面。 。因此,如果您想要的话,请在新页面上 driver.findElement(By.id("pin")).isDisplayed()要想成功,你必须诱导WebDriverWait对于WebElement根据我的答案的解决方案#3显示

关于java - 找不到此类元素错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48403496/

相关文章:

java - 是否可以在 android 中合并未知数量的 firestore 查询?

java - 将 XML 文件编码为 Spring bean

java - 如何用图像替换文本中的单词?

java - Selenium switchTo 返回错误 org.openqa.selenium.WebDriverException : unknown error: cannot determine loading status

internet-explorer - 网络驱动程序 | IE9 |设置自动下载文件

java - Morphia更新方式不明确

java - 如何在不使用 SoapHandler 的情况下通过 jaxb 将自定义 header 添加到 SOAP 请求

selenium - NoSuchElementException,Selenium 无法定位元素

java - 有没有办法获取在网站上导航时触发的所有控​​制台日志和事件?

java - element.getText() 方法在 java selenium 中不起作用