【发布时间】:2016-01-25 17:38:39
【问题描述】:
我正在创建一个测试来填写注册表单的前端,但是,我想单击下拉箭头选择值,然后检查所选值是否为预期值:我可以选择下拉列表中的值并输入值,但是,我遇到的问题是在检查所选值是否正确时。代码和错误如下:
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。
-
这些值不在选择中,这就是它不起作用的原因。它们在一个表中,值保存在如果这是选择 Menuitem6.selectByVisibleText("Mr");你想要的价值。对保存值的元素使用绝对 Xpath,然后使用 GetAttribute() 进行评估。我已经从下拉菜单中选择了先生。下拉列表是一个表格,值保存在
中,所以例外是元素应该是一个选择,但它是一个表格。选择后,我需要能够获取下拉列表的值。确认选择的值是正确的。 select 方法在这里作为一个表格元素不起作用。 您不能将 GetAttribute 与 select 一起使用。我的意思是使用表格元素的 Xpath 找到元素,然后使用 GetAttribute。
标签: java selenium selenium-webdriver