Java 如何使用 WebDriver 检查元素是否可见

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

How to check if an element is visible with WebDriver

javaseleniumwebdriver

提问by ponzao

With WebDriverfrom Selenium 2.0a2 I am having trouble checking if an element is visible.

随着WebDriver硒2.0a2我有麻烦检查如果一个元素是可见的。

WebDriver.findElementreturns a WebElement, which unfortunately doesn't offer an isVisiblemethod. I can go around this by using WebElement.clearor WebElement.clickboth of which throw an ElementNotVisibleException, but this feels very dirty.

WebDriver.findElement返回 a WebElement,不幸的是它没有提供isVisible方法。我可以通过使用WebElement.clearWebElement.click两者都抛出一个来解决这个问题ElementNotVisibleException,但这感觉很脏。

Any better ideas?

有什么更好的想法吗?

采纳答案by hleinone

element instanceof RenderedWebElementshould work.

element instanceof RenderedWebElement应该管用。

回答by Jason

It is important to see if the element is visible or not as the Driver.FindElementwill only check the HTML source. But popup code could be in the page html, and not be visible. Therefore, Driver.FindElementfunction returns a false positive (and your test will fail)

查看元素是否可见很重要,因为它Driver.FindElement只会检查 HTML 源。但是弹出代码可能在页面 html 中,并且不可见。因此,Driver.FindElement函数返回误报(并且您的测试将失败)

回答by sleske

Even though I'm somewhat late answering the question:

即使我回答这个问题有点晚:

You can now use WebElement.isDisplayed()to check if an element is visible.

您现在可以WebElement.isDisplayed()用来检查元素是否可见。

Note:

注意

There are many reasons why an element could be invisible. Selenium tries cover most of them, but there are edge cases where it does not work as expected.

元素不可见的原因有很多。Selenium 尝试覆盖其中的大部分,但在某些边缘情况下它无法按预期工作。

For example, isDisplayed()doesreturn falseif an element has display: noneor opacity: 0, but at least in my test, it does not reliably detect if an element is covered by another due to CSS positioning.

例如,如果一个元素有或,isDisplayed()确实会返回,但至少在我的测试中,由于 CSS 定位,它不能可靠地检测一个元素是否被另一个元素覆盖。falsedisplay: noneopacity: 0

回答by Ripon Al Wasim

I have the following 2 suggested ways:

我有以下两种建议的方法:

  1. You can use isDisplayed()as below:

    driver.findElement(By.id("idOfElement")).isDisplayed();
    
  2. You can define a method as shown below and call it:

    public boolean isElementPresent(By by) {
      try {
        driver.findElement(by);
        return true;
      }
    catch (org.openqa.selenium.NoSuchElementException e) {
        return false;
      }
    }
    
  1. 您可以使用isDisplayed()如下:

    driver.findElement(By.id("idOfElement")).isDisplayed();
    
  2. 您可以定义如下所示的方法并调用它:

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

Now, you can do assertion as below to check either the element is present or not:

现在,您可以进行如下断言以检查元素是否存在:

assertTrue(isElementPresent(By.id("idOfElement")));

回答by Christopher Bales

If you're using C#, it would be driver.Displayed. Here's an example from my own project:

如果您使用 C#,它将是 driver.Displayed。这是我自己项目中的一个例子:

if (!driver.FindElement(By.Name("newtagfield")).Displayed)      //if the tag options is not displayed
    driver.FindElement(By.Id("expand-folder-tags")).Click();    //make sure the folder and tags options are visible

回答by Adnan Ghaffar

Verifying ele is visible.

验证 ele 可见。

public static boolean isElementVisible(final By by)
    throws InterruptedException {
        boolean value = false;

        if (driver.findElements(by).size() > 0) {
            value = true;
        }
        return value;
    }

回答by Tarun Yaduvanshi

try this

尝试这个

public boolean isPrebuiltTestButtonVisible() {
    try {

        if (preBuiltTestButton.isEnabled()) {

            return true;

        } else {

            return false;
        }

    } catch (Exception e) {

        e.printStackTrace();
        return false;
    }
}

回答by Charlie Seligman

Here is how I would do it (please ignore worry Logger class calls):

这是我的方法(请忽略担心的 Logger 类调用):

public boolean isElementExist(By by) {
    int count = driver.findElements(by).size();
    if (count>=1) {
        Logger.LogMessage("isElementExist: " + by + " | Count: " + count, Priority.Medium);
        return true;
    }
    else {
        Logger.LogMessage("isElementExist: " + by + " | Could not find element", Priority.High);
        return false;
    }   
}

public boolean isElementNotExist(By by) {
    int count = driver.findElements(by).size();
    if (count==0) {
        Logger.LogMessage("ElementDoesNotExist: " + by, Priority.Medium);
        return true;
    }
    else {
        Logger.LogMessage("ElementDoesExist: " + by, Priority.High);
        return false;
    }   
}

public boolean isElementVisible(By by) {
    try {
        if (driver.findElement(by).isDisplayed()) {
            Logger.LogMessage("Element is Displayed: " + by, Priority.Medium);
            return true;
        }
    }
    catch(Exception e) {       
        Logger.LogMessage("Element is Not Displayed: " + by, Priority.High);
        return false;
    }       

    return false;
}

回答by akhilesh gulati

public boolean isElementFound( String text) {
        try{
            WebElement webElement = appiumDriver.findElement(By.xpath(text));
            System.out.println("isElementFound : true :"+text + "true");
        }catch(NoSuchElementException e){
            System.out.println("isElementFound : false :"+text);
            return false;
        }
        return true;
    }

    text is the xpath which you would be passing when calling the function.
the return value will be true if the element is present else false if element is not pressent

回答by Vishnu B S

    try{
        if( driver.findElement(By.xpath("//div***")).isDisplayed()){
          System.out.println("Element is Visible");
        }
}
catch(NoSuchElementException e){
   else{
     System.out.println("Element is InVisible");
        }
}