2016-01-22 84 views
0

我正在創建一個測試來填寫註冊表單的前端,但是,我想單擊下拉箭頭選擇值,然後檢查所選值是否爲預期值:我可以從下拉列表中選擇值並輸入值,但是,我所遇到的問題是檢查所選值是否正確。代碼和錯誤是如下:從控制檯Selenium Web-driver從下拉菜單中選擇索引值

driver.findElement(By.cssSelector("#dijit_form_Select_1 > tbody > tr > td.dijitReset.dijitRight.dijitButtonNode.dijitArrowButton.dijitDownArrowButton.dijitArrowButtonContainer > input")).click(); 


     WebElement Menuitem = driver.findElement(By.cssSelector("#dijit_MenuItem_6_text")); 

     boolean click1 = true; 

       Menuitem.click(); 

       //Checks if drop down button is selected 
       click1 = Menuitem.isSelected(); 

       if(click1 == false){ 
        System.out.println("DropDown Was Selected"); 
       }else { 
        System.out.println("DropDown was not clicked"); 
       } 



       Select Menuitem6 = new Select(driver.findElement(By.id("dijit_form_Select_1"))); 

       Menuitem6.selectByVisibleText("Mr"); 

       List<WebElement> list = Menuitem6.getOptions(); 

       for(int i=0;i<list.size();i++){ 
        if(list.get(i).getText().equals(Menuitem6.getFirstSelectedOption().getText())){ 
         System.out.println("The index of the selected option is: "+i); 
         break; 
         } 

錯誤:org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "table"

所以問題是明顯,這是因爲HTML元素是一個表,而不是一個選擇,我該如何解決這個問題得到什麼?

HTML代碼:

<tr aria-selected="false" widgetid="dijit_MenuItem_6" aria-disabled="false" aria-label="Mr " id="dijit_MenuItem_6" style="-moz-user-select: none;" class="dijitReset dijitMenuItem" data-dojo-attach-point="focusNode" role="option" tabindex="-1"> 
    <td class="dijitReset dijitMenuItemIconCell" role="presentation"> 
     <span role="presentation" class="dijitInline dijitIcon dijitMenuItemIcon dijitNoIcon" data-dojo-attach-point="iconNode"></span> 
    </td> 
    <td id="dijit_MenuItem_6_text" class="dijitReset dijitMenuItemLabel" colspan="2" data-dojo-attach-point="containerNode,textDirNode" role="presentation">Mr</td> 
    <td id="dijit_MenuItem_6_accel" class="dijitReset dijitMenuItemAccelKey" style="display: none" data-dojo-attach-point="accelKeyNode"></td> 
    <td class="dijitReset dijitMenuArrowCell" role="presentation"> 
     <span data-dojo-attach-point="arrowWrapper" style="visibility: hidden"> 
      <span class="dijitInline dijitIcon dijitMenuExpand"></span> 
      <span class="dijitMenuExpandA11y">+</span> 
     </span> 
    </td> 
+0

請發佈HTML。 – AlexCharizamhard

+0

這些值不在選擇中,這是它不工作的原因。它們在一個表格中,值保存在​​ – Speedychuck

+0

如果這是選擇Menuitem6.selectByVisibleText(「Mr」);你想要的價值。將絕對Xpath用於保存值的元素,然後使用GetAttribute()進行評估。 – AlexCharizamhard

回答

0

這似乎是很多工作,爲斷言選擇的值是正確的。

不是遍歷所有選項,而是選擇並使用GetAttribute(「Selected」)== true時,不只是引用相同的元素。特別是,如果該選擇功能正在處理該元素。

相關問題