我在自动化的网页上有一个dojo下拉列表。我必须单击显示的向下箭头键,然后会出现一个下拉列表。以下是相同的HTML。我尝试使用javascript executor处理它,我的代码片段如下所示。我确定这是错误的,因为我没有太多关于如何处理这种下拉菜单的信息 - 我正在尝试选择2个成人选项。网站是 - http://www.lufthansa.com/online/portal/lh/ua/homepage。下拉是乘客人数
WebElement select = driver.findElement(By.xpath("//*[@id='ns_7_CO19VHUC6N5PC0ACV6A7BG2I12_fmAdults']/tbody/tr/td[1]"));
select.click();
JavascriptExecutor js = (JavascriptExecutor) driver;
String jscript = "dojo.addOnLoad(function() { dijit.byId('ns_7_CO19VHUC6N5PC0ACV6A7BG2I12_fmAdults_menu').set('id','dijit_MenuItem_7_text') })";
js.executeScript(jscript);
HTML代码:
<div id="ns_7_CO19VHUC6N5PC0ACV6A7BG2I12_fmAdults_dropdown" class="dijitPopup dijitMenuPopup" style="visibility: visible; top: 307px; left: 953.5px; right: auto; z-index: 1000; height: auto; overflow: visible;" role="region" aria-label="ns_7_CO19VHUC6N5PC0ACV6A7BG2I12_fmAdults_menu" dijitpopupparent="ns_7_CO19VHUC6N5PC0ACV6A7BG2I12_fmAdults">
<table id="ns_7_CO19VHUC6N5PC0ACV6A7BG2I12_fmAdults_menu" class="dijit dijitReset dijitMenuTable dijitSelectMenu dijitValidationTextBoxMenu dijitMenuActive dijitMenu dijitMenuSelected dijitSelected dijitMenuHover dijitMenuSelectedHover dijitSelectedHover dijitHover dijitMenuFocused dijitMenuSelectedFocused dijitSelectedFocused dijitMenuHoverFocused dijitMenuSelectedHoverFocused dijitSelectedHoverFocused dijitHoverFocused dijitFocused" cellspacing="0" tabindex="-1" role="listbox" widgetid="ns_7_CO19VHUC6N5PC0ACV6A7BG2I12_fmAdults_menu" style="top: 0px; visibility: visible; width: 184px;" aria-labelledby="ns_7_CO19VHUC6N5PC0ACV6A7BG2I12_fmAdults">
<tbody class="dijitReset" data-dojo-attach-point="containerNode">
<tr id="dijit_MenuItem_2" class="dijitReset dijitSelectSelectedOption dijitValidationTextBoxSelectedOption dijitMenuItemSelected dijitMenuItem dijitMenuItemHover dijitHover dijitMenuItemFocused dijitMenuItemHoverFocused dijitHoverFocused dijitFocused" tabindex="0" role="option" data-dojo-attach-point="focusNode" style="-moz-user-select: none;" aria-label="2 Adults " aria-disabled="false" widgetid="dijit_MenuItem_2" aria-selected="true">
<td class="dijitReset dijitMenuItemIconCell" role="presentation">
<td id="dijit_MenuItem_2_text" class="dijitReset dijitMenuItemLabel" data-dojo-attach-point="containerNode,textDirNode" colspan="2">2 Adults </td>
<td id="dijit_MenuItem_2_accel" class="dijitReset dijitMenuItemAccelKey" data-dojo-attach-point="accelKeyNode" style="display: none"/>
<td class="dijitReset dijitMenuArrowCell" role="presentation">
</tr>
答案 0 :(得分:0)
无需使用Java Script Executor!,对于下拉表中的每个元素,它都位于&#39; dijitMenuItemLabel&#39; class,所以在一个List中获取所有这些元素并根据需要点击特定元素。 这是代码!
WebElement adultDrpDwnButton = driver.findElement(By.xpath(".//*[@id='ns_7_CO19VHUC6N5PC0ACV6A7BG2I12_fmAdults']/tbody/tr/td[2]"));
adultDrpDwnButton.click(); //click on the Adult Drop Down Button
WebDriverWait waitForDrpDown = new WebDriverWait(driver, 5);
waitForDrpDown.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='ns_7_CO19VHUC6N5PC0ACV6A7BG2I12_fmAdults_menu']/tbody")));
//after clicking the button it will wait for the drop down to be visible,if it is not visible in 5 Seconds, then it will throw an error
List<WebElement> drpListItems = driver.findElements(By.className("dijitMenuItemLabel"));
//now get all the element which is under 'dijitMenuItemLabel', there will be 6 elements in the List varibale drpListItems,
//you can check that by using drpListItems.size()
for(WebElement temp : drpListItems){ //now use special for loop and go through the each element and click on the particular element based on the reqiuirement
if(temp.getText().trim().equals("5 Adults")){
temp.click();
break;
}
}