簡體   English   中英

Selenium Web驅動程序從下拉菜單中按索引選擇值

[英]Selenium Web-driver Selecting value by index from drop-down menu

我正在創建一個用於填寫注冊表格前端的測試,但是,我想單擊下拉菜單中的箭頭以選擇該值,然后檢查所選值是否為預期值:我可以從以下選項中選擇該值:下拉菜單並輸入值,但是,在檢查所選值是否正確時,我遇到的問題是。 代碼和錯誤如下:

  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元素是一個表而不是一個select,如何解決這個問題?

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>

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

除了遍歷所有選項之外,為什么不直接引用與選擇並使用GetAttribute(“ Selected”)== true時相同的元素。 特別是,如果該選擇功能正在元素上運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM