我想用selenium java编写自动化测试。但我不知道如何从下拉列表中选择一个项目。 在html中,没有选择标记。下拉项(Auth ID,Correlation ID等)写在表中。我写了
WebElement dropDownElement2 = driver.findElement(By.xpath("//*[@id='dijit_MenuItem_12_text']"));
dropDownElement2.click();
但Chrome网络驱动程序说“元素不可见”
源代码如下。
<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>
答案 0 :(得分:0)
在从下拉列表中选择选项之前,首先我们需要点击它以使下拉列表可见。请找到以下代码:
// 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();
希望这有帮助。
答案 1 :(得分:-1)
制作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();
}