Java 元素不可见firefox webdriver异常

Java 元素不可见firefox webdriver异常,java,selenium,xpath,webdriver,Java,Selenium,Xpath,Webdriver,我已经通过xpath识别了一个WebElement,下面的代码与给定HTML代码中的相同 XPath表达式 //a[@id='close_bttn'] 文档 <form action="/gateway/orders" method="get"> <input class="dynamic-table-search-by" type="hidden" value="Date Range" name="searchBy"/> <input type="hidden

我已经通过xpath识别了一个WebElement,下面的代码与给定HTML代码中的相同

XPath表达式

//a[@id='close_bttn']
文档

<form action="/gateway/orders" method="get">
<input class="dynamic-table-search-by" type="hidden" value="Date Range" name="searchBy"/>

<input type="hidden" value="20" name="pageSize"/>
<div class="input-prepend input-append">

<a id="close_bttn" class="btn dynamic-table-search-clear" href="/gateway/orders?pageSize=20&totalItems=16024" type="button">
<i class="icon-remove dynamic-table-search-clear-icon"/>
</a>

运行WebDriver后,我发现错误如下

元素当前不可见,因此可能无法与命令持续时间或超时交互


我看到我正在处理的XPath没有添加隐藏标记。请告诉我是否有解决此问题的方法。

我想您应该使用WebDriveWait:

WebDriverWait waiting = new WebDriverWait(driver, X, Y);

WebElement element = waiting.until(ExpectedConditions.visibilityOfElementLocated(By.id("close_bttn")));

此代码将每Y毫秒检查一次预期条件(close_bttn对象的可见性)。等待时间为X秒。

以下是我可能的方法,使用双重检查,以确保:

   public boolean isElementThere( By locator ) { 
      boolean found = false;
      WebDriverWait waiting = new WebDriverWait(driver, 30, 2500);
      WebElement element;
      try {
        element = waiting.until(
            ExpectedConditions.visibilityOfElementLocated(locator));
        element = waiting.until(
            ExpectedConditions.presenceOfElementLocated(locator));
      } catch ( WebDriverException e ) {
         return false;
      }
      if ( element != null ) found = true;
      return found;
   }