java 如何使用 selenium 2 验证页面上的 Web 元素是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17470152/
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-11-01 02:05:36  来源:igfitidea点击:

How to verify web element presence on the page using selenium 2

javaselenium-webdriver

提问by Eugene

I'm trying to implement a test using selenium web-driver 2 tool.

我正在尝试使用 selenium web-driver 2 工具实施测试。

Application has elements, which presence is unpredictable. Ok. In the most cases they are present on the page. But in the certain cases they aren't. The following method clicks the unpredictable element

应用程序具有不可预测的元素。行。在大多数情况下,它们出现在页面上。但在某些情况下,它们不是。以下方法点击不可预知的元素

public void clickTypeAheadDropdown(String typeAheadItem) {
    String xPathItemSelector = "//div[@class='gwt-SuggestBoxPopup']//td[text()='" + typeAheadItem + "']";
    WebElement dropDownItem = driver.findElement(By.xpath(xPathItemSelector));
    if (dropDownItem.isDisplayed() ) {
        dropDownItem.click();
    };

}

but it fails when the element is absent. The exception is rised by the method driver.findElement(By.xpath(xPathItemSelector)

但是当元素不存在时它会失败。异常由方法driver.findElement(By.xpath(xPathItemSelector)

Do you know, how can I test, does element present on the page?

您知道吗,我如何测试页面上是否存在元素?

P.S. I assume, that catching "Element Not Found" exception is not a good idea, because it is raised when test is out of time

PS我认为,捕获“未找到元素”异常不是一个好主意,因为它是在测试超时时引发的

回答by LaurentG

I usually use the following method to test if an element is present.

我通常使用以下方法来测试元素是否存在。

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

The wait time can also be configured on the WebDriver:

等待时间也可以配置在WebDriver

webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

I don't know any other way to do this. Since your page could load with unpredictable time, you are force to wait and use a timeout.

我不知道还有什么其他方法可以做到这一点。由于您的页面可能会以不可预测的时间加载,因此您必须等待并使用超时。

回答by Tedesco

You can also do it using FindElements:

您也可以使用 FindElements 来完成:

    /// <summary>
    /// Checks if the specified element is on the page.
    /// </summary>
    public static bool IsElementPresent(this IWebDriver driver, By element)
    {
        if (driver.FindElements(element).Count > 0)
            return true;
        else
            return false;
    }

    /// <summary>
    /// Checks if the specified element is on the page and is displayed.
    /// </summary>
    public static bool IsElementDisplayed(this IWebDriver driver, By element)
    {
        if (driver.FindElements(element).Count > 0)
        {
            if (driver.FindElement(element).Displayed)
                return true;
            else
                return false;
        }
        else
        {
            return false;
        }
    }

    /// <summary>
    /// Checks if the specified element is on the page and is enabled.
    /// </summary>
    public static bool IsElementEnabled(this IWebDriver driver, By element)
    {
        if (driver.FindElements(element).Count > 0)
        {
            if (driver.FindElement(element).Enabled)
                return true;
            else
                return false;
        }
        else
        {
            return false;
        }
    }

Hope it helps.

希望能帮助到你。

回答by Pradnya Kanase

To check if element is Present you can use following code:

要检查元素是否存在,您可以使用以下代码:

if(driver.findElements(By.xpath("value")).size() != 0){
System.out.println("Element is Present");
}else{
System.out.println("Element is Absent");
}