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>
请发布HTML。 – AlexCharizamhard
这些值不在选择中,这是它不工作的原因。它们在一个表格中,值保存在 – Speedychuck
如果这是选择Menuitem6.selectByVisibleText(“Mr”);你想要的价值。将绝对Xpath用于保存值的元素,然后使用GetAttribute()进行评估。 – AlexCharizamhard