如何使用硒从下拉列表中选择一个项目?-没有选择标签
How to select an item from drop-down list using selenium? - no select tag
我想使用selenium java编写自动化测试。但是我不知道如何从下拉列表中选择项目。在html中,没有选择标记。下拉项(Auth ID、Correlation ID等)写在一个表中。我写了
WebElement dropDownElement2 = driver.findElement(By.xpath("//*[@id='dijit_MenuItem_12_text']"));
dropDownElement2.click();
但是Chrome浏览器驱动程序显示异常"Element not visible"
源代码如下:
<document>
<html class="dj_gecko dj_ff45 dj_contentbox">
<head>
<body class="oneui claro">
<div id="re_app" widgetid="re_app">
<noscript> <style type="text/css"> #re_app { display: none; } h2 { max-width: 1024px; margin: 0 auto; padding-top: 40px; text-align: center; } </style> <div> <h2>Javascript is required to run the web application. <br>Please, turn it on and reload the page.</h2> </div> </noscript>
<div id="uniqName_3_0" class="dijitDialog" aria-labelledby="uniqName_3_0_title" role="dialog" widgetid="uniqName_3_0" style="position: absolute; opacity: 0; left: 636px; top: 337px; z-index: 950; display: none;">
<div id="uniqName_2_12_dropdown" class="dijitPopup dijitMenuPopup" style="visibility: visible; top: 0px; z-index: 1000; right: auto; left: 774.833px; display: none;" role="region" aria-label="uniqName_2_12_menu" dijitpopupparent="uniqName_2_12">
<div id="uniqName_2_12_menu" class="dijit dijitMenu dijitReset dijitSelectMenu dijitValidationTextBoxMenu dijitMenuPassive" style="overflow: visible; top: 0px; visibility: visible; width: 451px;" role="listbox" widgetid="uniqName_2_12_menu" aria-labelledby="uniqName_2_12" tabindex="0">
<table class="dijitReset dijitMenuTable" cellspacing="0" data-dojo-attach-event="onkeypress:_onKeyPress" tabindex="0" role="presentation" style="">
<tbody class="dijitReset" data-dojo-attach-point="containerNode">
<tr id="dijit_MenuSeparator_1" class="dijitMenuSeparator" role="separator" style="-moz-user-select: none;" widgetid="dijit_MenuSeparator_1" tabindex="-1" aria-selected="false">
<td class="dijitMenuSeparatorIconCell">
<div class="dijitMenuSeparatorTop"/>
<div class="dijitMenuSeparatorBottom"/>
</td>
<td class="dijitMenuSeparatorLabelCell" colspan="3">
<div class="dijitMenuSeparatorTop dijitMenuSeparatorLabel"/>
<div class="dijitMenuSeparatorBottom"/>
</td>
</tr>
<tr id="dijit_MenuItem_11" class="dijitReset dijitSelectSelectedOption dijitValidationTextBoxSelectedOption dijitMenuItem" tabindex="-1" role="option" data-dojo-attach-point="focusNode" aria-labelledby="dijit_MenuItem_11_text dijit_MenuItem_11_accel" style="-moz-user-select: none;" aria-disabled="false" widgetid="dijit_MenuItem_11" aria-selected="true">
<td class="dijitReset dijitMenuItemIconCell" role="presentation">
<td id="dijit_MenuItem_11_text" class="dijitReset dijitMenuItemLabel" data-dojo-attach-point="containerNode" colspan="2">Auth ID</td>
<td id="dijit_MenuItem_11_accel" class="dijitReset dijitMenuItemAccelKey" data-dojo-attach-point="accelKeyNode" style="display: none"/>
<td class="dijitReset dijitMenuArrowCell" role="presentation">
<div style="visibility: hidden" data-dojo-attach-point="arrowWrapper">
<img class="dijitMenuExpand" alt="" src="dojo/dojo/resources/blank.gif"/>
<span class="dijitMenuExpandA11y">+</span>
</div>
</td>
</tr>
<tr id="dijit_MenuItem_12" class="dijitReset dijitMenuItem" tabindex="-1" role="option" data-dojo-attach-point="focusNode" aria-labelledby="dijit_MenuItem_12_text dijit_MenuItem_12_accel" style="-moz-user-select: none;" aria-disabled="false" widgetid="dijit_MenuItem_12" aria-selected="false">
<td class="dijitReset dijitMenuItemIconCell" role="presentation">
<td id="dijit_MenuItem_12_text" class="dijitReset dijitMenuItemLabel" data-dojo-attach-point="containerNode" colspan="2">Correlation ID</td>
<td id="dijit_MenuItem_12_accel" class="dijitReset dijitMenuItemAccelKey" data-dojo-attach-point="accelKeyNode" style="display: none"/>
<td class="dijitReset dijitMenuArrowCell" role="presentation">
</tr>
<tr id="dijit_MenuItem_13" class="dijitReset dijitMenuItem" tabindex="-1" role="option" data-dojo-attach-point="focusNode" aria-labelledby="dijit_MenuItem_13_text dijit_MenuItem_13_accel" style="-moz-user-select: none;" aria-disabled="false" widgetid="dijit_MenuItem_13" aria-selected="false">
<tr id="dijit_MenuItem_14" class="dijitReset dijitMenuItem" tabindex="-1" role="option" data-dojo-attach-point="focusNode" aria-labelledby="dijit_MenuItem_14_text dijit_MenuItem_14_accel" style="-moz-user-select: none;" aria-disabled="false" widgetid="dijit_MenuItem_14" aria-selected="false">
<tr id="dijit_MenuItem_15" class="dijitReset dijitMenuItem" tabindex="-1" role="option" data-dojo-attach-point="focusNode" aria-labelledby="dijit_MenuItem_15_text dijit_MenuItem_15_accel" style="-moz-user-select: none;" aria-disabled="false" widgetid="dijit_MenuItem_15" aria-selected="false">
<tr id="dijit_MenuItem_16" class="dijitReset dijitMenuItem" tabindex="-1" role="option" data-dojo-attach-point="focusNode" aria-labelledby="dijit_MenuItem_16_text dijit_MenuItem_16_accel" style="-moz-user-select: none;" aria-disabled="false" widgetid="dijit_MenuItem_16" aria-selected="false">
<tr id="dijit_MenuItem_17" class="dijitReset dijitMenuItem" tabindex="-1" role="option" data-dojo-attach-point="focusNode" aria-labelledby="dijit_MenuItem_17_text dijit_MenuItem_17_accel" style="-moz-user-select: none;" aria-disabled="false" widgetid="dijit_MenuItem_17" aria-selected="false">
<tr id="dijit_MenuItem_18" class="dijitReset dijitMenuItem" tabindex="-1" role="option" data-dojo-attach-point="focusNode" aria-labelledby="dijit_MenuItem_18_text dijit_MenuItem_18_accel" style="-moz-user-select: none;" aria-disabled="false" widgetid="dijit_MenuItem_18" aria-selected="false">
<tr id="dijit_MenuItem_19" class="dijitReset dijitMenuItem" tabindex="-1" role="option" data-dojo-attach-point="focusNode" aria-labelledby="dijit_MenuItem_19_text dijit_MenuItem_19_accel" style="-moz-user-select: none;" aria-disabled="false" widgetid="dijit_MenuItem_19" aria-selected="false">
<tr id="dijit_MenuItem_20" class="dijitReset dijitMenuItem" tabindex="-1" role="option" data-dojo-attach-point="focusNode" aria-labelledby="dijit_MenuItem_20_text dijit_MenuItem_20_accel" style="-moz-user-select: none;" aria-disabled="false" widgetid="dijit_MenuItem_20" aria-selected="false">
<tr id="dijit_MenuItem_21" class="dijitReset dijitMenuItem" tabindex="-1" role="option" data-dojo-attach-point="focusNode" aria-labelledby="dijit_MenuItem_21_text dijit_MenuItem_21_accel" style="-moz-user-select: none;" aria-disabled="false" widgetid="dijit_MenuItem_21" aria-selected="false">
<tr id="dijit_MenuItem_22" class="dijitReset dijitMenuItem" tabindex="-1" role="option" data-dojo-attach-point="focusNode" aria-labelledby="dijit_MenuItem_22_text dijit_MenuItem_22_accel" style="-moz-user-select: none;" aria-disabled="false" widgetid="dijit_MenuItem_22" aria-selected="false">
<tr id="dijit_MenuItem_23" class="dijitReset dijitMenuItem" tabindex="-1" role="option" data-dojo-attach-point="focusNode" aria-labelledby="dijit_MenuItem_23_text dijit_MenuItem_23_accel" style="-moz-user-select: none;" aria-disabled="false" widgetid="dijit_MenuItem_23" aria-selected="false">
<tr id="dijit_MenuItem_24" class="dijitReset dijitMenuItem" tabindex="-1" role="option" data-dojo-attach-point="focusNode" aria-labelledby="dijit_MenuItem_24_text dijit_MenuItem_24_accel" style="-moz-user-select: none;" aria-disabled="false" widgetid="dijit_MenuItem_24" aria-selected="false">
</tbody>
</table>
</div>
<iframe class="dijitBackgroundIframe" src="javascript:""" role="presentation" style="opacity: 0.1; width: 100%; height: 100%;" tabindex="-1"/>
</div>
</body>
</html>
</document>
在从下拉列表中选择选项之前,首先我们需要单击它以使下拉列表可见。请查看下面的代码:
// Clicking on dropdown
driver.findElement(By.xpath(<locator to find the dropdown>)).click();
// Clicking of the list item
WebElement dropDownElement2 = driver.findElement(By.xpath("//*[@id='dijit_MenuItem_12_text']"));
dropDownElement2.click();
创建tr/td标签列表,并在for循环中单击所需的
List<WebElement> TRCollection = driver.findElement(By.xpath("//table[@class='dijitReset dijitMenuTable']")).findElements(By.tagName("tr"));
List<WebElement> TDCollection;
// if you have td's in tr than you need to loop that way
for(int i = 0 ; i < TRCollection.size() ; i++)
{
TDCollection = TRCollection.get(i).findElements(By.tagName("td"));
//look at the elements you get and try to click. for example element index 0
TDCollection.get(0).click()
}
//if only tr
for(int i = 0 ; i < TRCollection.size() ; i++)
{
TRCollection.get(i).click();
}
相关文章:
- 当在Ember中点击一个项目时,我如何将一个活动类添加到项目列表中
- 通过另一个php应用程序将我的项目推送到Github存储库中
- angularjs移除焦点上的活动类并添加到下一个项目
- 相对于角度控制器中的另一个阵列过滤阵列项目
- 如何通过AngularJS删除一个项目
- 如何使用jquery将所选项目从一个下拉组列表(optgroup)移动到另一个下拉列表(optgroup)
- 获取每个项目中不为 null 的最后一个项目
- 在另一个项目中使用大理石测试rxjs5方法
- 使用javascript/jquery从现有数组中创建一个新数组,该数组保存项目存在的次数
- 如何在猫头鹰旋转木马中滑动所有可见项目,而不是使用上一个/下一个按钮
- Git为整个项目提供一个repo(frontend+node.js)
- 与杜兰达尔合作的第一个JavaScript项目.尝试从第三方 API 获取数据
- Asp.net 引导下拉菜单 - 选择一个项目
- 处理一个项目,想要添加以下菜单栏,并尽量减少向下滑动
- 如何将从GWT编译的JS添加/访问到另一个外部HTML / JS项目中
- 在angularJs的选择框中预先选择一个项目
- 我想在混合移动应用程序中使用Sqlite插件(Cordova)创建一个示例项目
- 如何将一个表单中的项目添加到 Rails 中另一个表单的下拉选项
- jQuery 使用 ID 作为 URL 选择一个项目
- 淡入<灯光>一个项目一个项目