繁体   English   中英

使用带有 Java 的 Selenium WebDriver 检查元素不存在的最佳方法

[英]Best way to check that element is not present using Selenium WebDriver with java

我正在尝试下面的代码,但它似乎不起作用......有人可以告诉我最好的方法吗?

public void verifyThatCommentDeleted(final String text) throws Exception {
    new WebDriverWait(driver, 5).until(new ExpectedCondition<Boolean>() {
            @Override
            public Boolean apply(WebDriver input) {
                try {
                    input.findElement(By.xpath(String.format(
                            Locators.CHECK_TEXT_IN_FIRST_STATUS_BOX, text)));
                    return false;
                } catch (NoSuchElementException e) {
                    return true;
                }
            }
        });
    }

不是执行 findElement,而是执行 findElements 并检查返回元素的长度是否为 0。这就是我使用 WebdriverJS 的方式,我希望在 Java 中也能正常工作

我通常有几种方法(成对)来验证元素是否存在:

public boolean isElementPresent(By locatorKey) {
    try {
        driver.findElement(locatorKey);
        return true;
    } catch (org.openqa.selenium.NoSuchElementException e) {
        return false;
    }
}

public boolean isElementVisible(String cssLocator){
    return driver.findElement(By.cssSelector(cssLocator)).isDisplayed();
}

请注意,有时 selenium 可以找到 DOM 中的元素,但它们可能不可见,因此 selenium 将无法与它们交互。 因此,在这种情况下,检查可见性的方法会有所帮助。

如果你想等待元素直到它出现,我发现最好的解决方案是使用流畅的等待:

public WebElement fluentWait(final By locator){
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
            .withTimeout(30, TimeUnit.SECONDS)
            .pollingEvery(5, TimeUnit.SECONDS)
            .ignoring(NoSuchElementException.class);

    WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
        public WebElement apply(WebDriver driver) {
            return driver.findElement(locator);
        }
    });

    return foo;
};

希望这可以帮助)

使用findElements而不是findElement

如果没有找到匹配的元素而不是异常, findElements将返回一个空列表。 此外,我们可以确保该元素是否存在。

例如:列表元素 = driver.findElements(By.yourlocatorstrategy);

if(elements.size()>0){
    do this..
 } else {
    do that..
 }

无法评论流星测试手册,因为我没有代表,但我想提供一个例子,让我花了很长时间才弄清楚:

Assert.assertEquals(0, wd.findElements(By.locator("locator")).size());

此断言验证 DOM 中没有匹配的元素并返回零值,因此当元素不存在时断言通过。 如果它存在,它也会失败。

int i=1;

while (true) {
  WebElementdisplay=driver.findElement(By.id("__bar"+i+"-btnGo"));
  System.out.println(display);

  if (display.isDisplayed()==true)
  { 
    System.out.println("inside if statement"+i);
    driver.findElement(By.id("__bar"+i+"-btnGo")).click();
    break;
  }
  else
  {
    System.out.println("inside else statement"+ i);
    i=i+1;
  }
}
WebElement element = driver.findElement(locator);
Assert.assertNull(element);

如果 element 不存在,则上述断言将通过。

public boolean isDisplayed(WebElement element) {
        try {
            return element.isDisplayed();
        } catch (NoSuchElementException e) {
            return false;
        }
    }

如果您想检查该元素是否显示在页面上,您的检查应该是:

if(isDisplayed(yourElement){
...
}
else{
...
}

在 Python 中我使用断言:

assert len(driver.find_elements_by_css_selector("your_css_selector")) == 0

我的答案要简单得多,对我有用

while(true) {
if (driver.getPageSource().contains("Enter the text of the element you wanted to check")==false) 
{//Put your code here 
    break;
}   
else if (isElementPresent(element you are trying to find)==true)
{
                //put your code here 
                break;
}

}

    WebElement element = driver.findElement(locator);

    Assert.assertFalse(element.isDisplayed());

如果元素不存在,则断言将通过,否则将失败。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM