使用带有 Java 的 Selenium WebDriver 断言 WebElement 不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3283310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 19:19:24  来源:igfitidea点击:

Assert that a WebElement is not present using Selenium WebDriver with java

javaselenium-webdriverassertion

提问by True_Blue

In tests that I write, if I want to assert a WebElement is present on the page, I can do a simple:

在我编写的测试中,如果我想断言页面上存在 WebElement,我可以做一个简单的:

driver.findElement(By.linkText("Test Search"));

This will pass if it exists and it will bomb out if it does not exist. But now I want to assert that a link does notexist. I am unclear how to do this since the code above does not return a boolean.

如果它存在,它将通过,如果它不存在,它将被炸毁。但现在我想断言的链接本身并不存在。我不清楚如何做到这一点,因为上面的代码不返回布尔值。

EDITThis is how I came up with my own fix, I'm wondering if there's a better way out there still.

编辑这就是我想出我自己的修复方法,我想知道是否还有更好的方法。

public static void assertLinkNotPresent (WebDriver driver, String text) throws Exception {
List<WebElement> bob = driver.findElements(By.linkText(text));
  if (bob.isEmpty() == false) {
    throw new Exception (text + " (Link is present)");
  }
}

采纳答案by Andre

Not Sure which version of selenium you are referring to, however some commands in selenium * can now do this: http://release.seleniumhq.org/selenium-core/0.8.0/reference.html

不确定您指的是哪个版本的 selenium,但是 selenium * 中的一些命令现在可以执行此操作:http: //release.seleniumhq.org/selenium-core/0.8.0/reference.html

  • assertNotSomethingSelected
  • assertTextNotPresent
  • assertNotSomethingSelected
  • 断言文本不存在

Etc..

等等..

回答by Sergii Pozharov

I think that you can just catch org.openqa.selenium.NoSuchElementExceptionthat will be thrown by driver.findElementif there's no such element:

我认为你可以抓住如果没有这样的元素org.openqa.selenium.NoSuchElementException就会抛出driver.findElement

import org.openqa.selenium.NoSuchElementException;

....

public static void assertLinkNotPresent(WebDriver driver, String text) {
    try {
        driver.findElement(By.linkText(text));
        fail("Link with text <" + text + "> is present");
    } catch (NoSuchElementException ex) { 
        /* do nothing, link is not present, assert is passed */ 
    }
}

回答by Ripon Al Wasim

boolean titleTextfield = driver.findElement(By.id("widget_polarisCommunityInput_113_title")).isDisplayed();
assertFalse(titleTextfield, "Title text field present which is not expected");

回答by some_other_guy

Try this -

尝试这个 -

private boolean verifyElementAbsent(String locator) throws Exception {
    try {
        driver.findElement(By.xpath(locator));
        System.out.println("Element Present");
        return false;

    } catch (NoSuchElementException e) {
        System.out.println("Element absent");
        return true;
    }
}

回答by some_other_guy

findElement will check the html source and will return true even if the element is not displayed. To check whether an element is displayed or not use -

findElement 将检查 html 源,即使该元素未显示也会返回 true。要检查元素是否显示,请使用 -

private boolean verifyElementAbsent(String locator) throws Exception {

        boolean visible = driver.findElement(By.xpath(locator)).isDisplayed();
        boolean result = !visible;
        System.out.println(result);
        return result;
}

回答by Sarhanis

It's easier to do this:

这样做更容易:

driver.findElements(By.linkText("myLinkText")).size() < 1

回答by Fabian Barney

There is an Class called ExpectedConditions:

有一个类叫做ExpectedConditions

  By loc = ...
  Boolean notPresent = ExpectedConditions.not(ExpectedConditions.presenceOfElementLocated(loc)).apply(getDriver());
  Assert.assertTrue(notPresent);

回答by Petr Mensik

You can utlilize Arquillian Grapheneframework for this. So example for your case could be

您可以为此使用Arquillian Graphene框架。所以你的情况的例子可能是

Graphene.element(By.linkText(text)).isPresent().apply(driver));

Is also provides you bunch of nice API's for working with Ajax, fluent waits, page objects, fragments and so on. It definitely eases a Selenium based test development a lot.

它还为您提供了一堆很好的 API,用于处理 Ajax、流畅的等待、页面对象、片段等。它无疑大大简化了基于 Selenium 的测试开发。

回答by user1732136

With Selenium Webdriver would be something like this:

使用 Selenium Webdriver 将是这样的:

assertTrue(!isElementPresent(By.linkText("Empresas en Misión")));

回答by QualiT

For node.js I've found the following to be effective way to wait for an element to no longer be present:

对于 node.js,我发现以下是等待元素不再存在的有效方法:

// variable to hold loop limit
    var limit = 5;
// variable to hold the loop count
    var tries = 0;
        var retry = driver.findElements(By.xpath(selector));
            while(retry.size > 0 && tries < limit){
                driver.sleep(timeout / 10)
                tries++;
                retry = driver.findElements(By.xpath(selector))
            }