首页 > 解决方案 > org.openqa.selenium.NoSuchElementException:在检查条件时,使用给定的搜索参数无法在页面上找到一个元素

问题描述

我正在编写一个 if 条件来查找项目中的元素位置。

    if (!(driver.findElement(MobileBy.xpath(ObjRepoProp.getProperty("searchTextBox_XPATH"))).isDisplayed()
            || driver.findElement(MobileBy.xpath(ObjRepoProp.getProperty("routeOverview_XPATH"))).isDisplayed()
            || driver.findElement(MobileBy.xpath(ObjRepoProp.getProperty("scrollNavigationDrawer_XPATH")))
                    .isDisplayed())) {
        throw new IllegalStateException("This is not Destination Input page");
    }

在此代码中,如果条件之一为真,则“org.openqa.selenium.NoSuchElementException:使用给定的搜索参数无法在页面上找到元素”。触发器和测试用例失败。

标签: javaseleniumif-statement

解决方案


让我们首先了解这是如何工作的。

您的代码发生了两件事。

  1. 您正在尝试在网页上查找元素
  2. 拥有元素后,您将验证它是否可见。

因此,如果所有元素都在页面上找到并找到,那么只有它会起作用,否则你总是会得到NoSuchElementException.


推荐阅读