简体   繁体   English

Selenium Web驱动程序从下拉菜单中按索引选择值

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

I am creating a test to fill out the front end of a registration form, however, I want to click on the arrow for the drop down select the value and then Check that the selected value is the value expected: I can select the value from the dropdown and enter the value, however, the issue I am having is when coming to check that the value selected is the correct one. 我正在创建一个用于填写注册表格前端的测试,但是,我想单击下拉菜单中的箭头以选择该值,然后检查所选值是否为预期值:我可以从以下选项中选择该值:下拉菜单并输入值,但是,在检查所选值是否正确时,我遇到的问题是。 Code and error is below: 代码和错误如下:

  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;
                        }

Error from console: org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "table" 来自控制台的错误: org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "table"

So the issue is clearly that it is because the html element is a table and not a select, how do I get around this issue? 所以问题很明显是因为html元素是一个表而不是一个select,如何解决这个问题?

HTML Code: 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>

This seems to be a lot of work for asserting that the value selected is correct. 断言所选择的值正确似乎是很多工作。

Instead of iterating through all options why not just refer to the same element as when you selected and use GetAttribute("Selected") == true. 除了遍历所有选项之外,为什么不直接引用与选择并使用GetAttribute(“ Selected”)== true时相同的元素。 Especially, if that select function is working on the element. 特别是,如果该选择功能正在元素上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM