2017-05-05 57 views
2

我有一個帶有md-sidenav的網頁,我試圖用硒進行測試。除了當我嘗試點擊md-sidenav中的元素時,我在頁面上做的所有工作。當使用driver.findElement(By.name(「elementName」)或id訪問md-sidenav中的web元素時,我發現元素不是null。另外,當訪問an時,我可以訪問它的所有子元素,元素但當我嘗試點擊他們,它給了我一個ElementNotVisibleException:不能點擊元素Selenium如何在md-sidenav中點擊

下面就爲HTML:

<body ng-controller='MyController as my' ng-cloak> 
<section layout="row" column> 
    <md-sidenav id="sideNavRight" class="md-sidenav-right" md-component-id="right" ng-init="my.selectTab(1)"> 

    <div ng-controller="MyController" ng-show="my.isSelected(1)"> 
     <md-toolbar class="md-theme-light"> 
      <h1 class="md-toolbar-tools">Some Form</small> 
      <md-button id="navClose" ng-click="close()" class="closeIcon" aria-label="Close"> 
       <md-icon md-svg-icon="assets/img/ic_close_white_24px.svg"></md-icon><sr-only>Close</sr-only> 
      </md-button></h1> 
     </md-toolbar> 

     <md-content> 
      <div layout="column" index="0"> 
      <h3>Some Information</h3> <br /> 
       <form name="myForm"> 
        <div layout="row" layout-align="start" flex="100"> 
         <md-input-container flex=""> 
          <label>Category:</label> 
           <md-select name="category" required="" ng-model="menu.category" > 
            <md-option class="choices" value="cat1">Category1</md-option> 
            <md-option value="cat2">Category2</md-option> 
            <md-option value="cat3">Category3</md-option> 
           </md-select> 
           <div class="errors" ng-messages="myForm.category.$error"> 
            <div ng-message="required">Required</div> 
           </div> 
         </md-input-container> 
        </div> 
         <div layout="row" class="pull-right"> 
          <md-button class="btn" ng-click="clearValue()">Clear</md-button> &nbsp; 
          <md-button class="btn" ng-click="menu.updateCart();menu.selectTab(2);">Next</md-button> 
         </div> 
       </form> 
      </div> 
     </md-content> 
    </div> 
    </md-sidenav> 
    </section> 

    .... 
    rest of the page 
    .... 
</body> 

這裏是硒代碼:

import java.util.List; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.ie.InternetExplorerDriver; 
import org.openqa.selenium.support.ui.ExpectedCondition; 
import org.openqa.selenium.support.ui.Select; 
import org.openqa.selenium.support.ui.Wait; 
import org.openqa.selenium.support.ui.WebDriverWait; 

public class EcsTest { 
    static WebDriver driver; 
    static Wait<WebDriver> wait; 
    static String prevText = ""; 
    static String currText = ""; 

    public static void main(String[] args) { 
     System.setProperty("webdriver.ie.driver","C:\\Dvp\\Irs\\EcsTest\\IEDriverServer.exe"); 
     driver = new InternetExplorerDriver(); 
     wait = new WebDriverWait(driver, 30); 
     driver.get("http://localhost:8080/ecs/index.html"); 

     try { 
      driver.findElement(By.id("element1")).click(); 

      wait.until(new ExpectedCondition<Boolean>() { 
       @Override 
       public Boolean apply(WebDriver webDriver) { 
        currText = webDriver.findElement(By.id("field1")).getText(); 
        return !prevText.equals(currText); 
       } 
      }); 

      prevText = currText; 

      driver.findElement(By.id("element2")).click(); 
      //driver.findElement(By.name("dispositionSelect")).sendKeys("Add to Scheme"); 

      Select selectElement = new Select(driver.findElement(By.name("selectElement1"))); 
      disposition.selectByVisibleText("Some form"); 

      WebElement sideNav = driver.findElement(By.id("sideNavRight")); 
      WebElement closeBtn = sideNav.findElement(By.id("navClose")); 

      /* 
       This prints out: 
       sideNav is not null!, closeBtn is not null!, not displayed!, enabled! 
      */ 
      System.out.println(
        "sideNav is " + ((sideNav == null) ? "null!" : "not null!") + 
        ", closeBtn is " + ((closeBtn == null) ? "null!" : "not null!") + 
        ", " + ((closeBtn.isDisplayed()) ? "displayed!" : "not displayed!") + 
        ", " + ((closeBtn.isEnabled()) ? "enabled!" : "not enabled!")); 


      WebElement select = sideNav.findElement(By.name("category")); 
      List<WebElement> options = select.findElements(By.tagName("md-option")); 

      /* 
       This prints out: 
       select: category, visible: false 
      */ 
      System.out.println(
       "select: " + select.getAttribute("name") + ", visible: " + select.isDisplayed()); 

      /* 
       This crashes: 
       ElementNotVisibleException: Cannot click on element 

       Without this click, I can see the options fine but the option click crashes 
      */ 
      select.click(); 

      for(WebElement option : options) { 
       System.out.println("option: " + option.getAttribute("value")); 

       if(option.getAttribute("value").equals(optionName)) { 
        option.click(); 
        System.out.println("option: " + option.getAttribute("value") + " clicked!"); 
        break; 
       } 
      } 
     } catch (Exception exp) { 
      exp.printStackTrace(); 
     } 
     finally { 
      driver.close(); 
     } 
    } 
} 

任何幫助將不勝感激!

+0

一些建議的檢查。 (1)嘗試另一個瀏覽器,MSIE驅動程序已知是片狀 - 尤其是使用Actions,所以在Chrome中試試你的答案? (2)嘗試等待10秒,打開側導航並點擊它。 (3)添加更多的調試輸出 - sideNav是否可見? (4)嘗試點擊關閉按鈕元素下的''元素。 –

回答

0

創建一個操作類:

Actions builder = new Actions(driver); 

而且只要你在MD-sidenav是用它爲每一個操作:

WebElement yourElement = sideNav.findElement(By.id("elementId")); 
builder.moveToElement(yourElement).click().build().perform(); 
builder.moveToElement(yourElement).sendKeys("2017-05-04").build().perform(); 

等等

編輯: 這隻會導致操作不會拋出異常,但實際上不會執行操作。即它報告它點擊了一個按鈕,沒有發生異常,但也沒有采取任何行動。 我太快快樂了。

1

嘗試加入一些等待上述元素可見:

WebDriverWait wait = new WebDriverWait (driver,20); 
wait.until(ExpectedConditions.visibilityOf(select)); 

select.click(); 

如果仍然無法單擊該元素,然後使用Actions class如下答案的人提到嘗試。