简体   繁体   中英

How to select an item from drop-down list using selenium? - no select tag

I want to write automation testing using selenium java. But I don't know how to select an item from a drop down list. In the html, there isn't select tag.The drop down items(Auth ID, Correlation ID and so on) are written in a table. I wrote

WebElement dropDownElement2 = driver.findElement(By.xpath("//*[@id='dijit_MenuItem_12_text']"));
dropDownElement2.click();

But the Chrome webdriver says the exception "Element not visible"

The source code is as below.

    <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>

Before selecting the options from the dropdown, first we need to click on it to make the dropdown list visible. Please find the below code:

// 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();

Hope this helps.

make list of tr/td tags and in for loop clickon what you need

    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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM