Selenium Webdriver cannot choose a value in a dropdown list

1,135 views
Skip to first unread message

keylogger

unread,
Mar 23, 2015, 2:07:42 PM3/23/15
to webd...@googlegroups.com

Hi, 

I am trying to implement Selenium Webdriver using Java. 

Basically, I have a website with a button. Once user click that button, a small section below the button will be loaded (using AJAX).  

That loaded section contains a blank field. Once user click on the field, a drop-down list with 5 options will appear and user should choose one option. 

The HTML codes look like this

<!-- language: lang-html -->
<div class="default-form w-border scheduleAddFrom" style="display: block;">
<div>
<div class="section frameless nopadding nomargin" data-form-element="SectionHeading" style="min-width: 100%;">
<div class="section-body frameless nopadding nomargin">
<div class="default-form">
<div class="form-row required-message hidden" style="min-height: 25px;">
<div class="form-row print-avoid-page-break" data-form-element="FieldEdit" style="min-height: 25px;">
<label for="">Department</label>
<input id="Schedule-00-Row136153aa-9fa8-499b-8458-2b155443223bE-TaskId-Display" class="ui-autocomplete-display validate widget" type="text" autocomplete="off">
<span class="ui-autocomplete-display-icon"></span>
<div class="subhidden">
<select id="Schedule-00-Row136153aa-9fa8-499b-8458-2b155443223bE-TaskId" class="validate widget " data-default-value="" tabindex="5000" data-display-id="Schedule-00-Row136153aa-9fa8-499b-8458-2b155443223bE-TaskId-Display">
   
<option value=""></option>
   
<option value="OPT1">Option 1</option>
   
<option value="OPT2">Option 2</option>
   
<option value="OPT3">Option 3</option>
   
<option value="OPT4">Option 4</option>
   
<option value="OPT5">Option 5</option>
</select>

I use this Selenium Java code 


WebDriverWait wait = new WebDriverWait(driver, 100);

wait
.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".form-row.print-avoid-page-break>label")));

//Start to find the element. The ID is dynamically randomly generated by the system each time the page loads except the last part TaskID, thus looking for the string TaskID  
Select dropdown = new Select (driver.findElement(By.xpath(".//*[contains(@id,'TaskId')]")));

dropdown
.selectByValue("OPT2");


But Selenium returns error.

org.openqa.selenium.ElementNotVisibleException: element not visible: Element is not currently visible and may not be manipulated


So, I tried to change the code to 


JavascriptExecutor js = (JavascriptExecutor) driver; 
   js.executeScript("document.querySelector(\"select[id*='TaskId']>option[value='OPT2']\").click();");


Selenium now doesn't return error, but it also doesn't choose the Option 2 value in the drop-down list. 

I have a feeling that this is caused by <div class="subhidden">, but I am not too sure. Any suggestion is highly appreciated. Thanks.

 

darrell

unread,
Mar 24, 2015, 9:34:37 AM3/24/15
to webd...@googlegroups.com
Your code is using WebDriverWait to wait for the label "Department" to be visible. It then attempts to select an option from the select. You did not give the line which failed but I suspect it is failing when you attempt to do the select. Solution, you need a WebDriverWait in order to wait for the option to exist on the select statement. I tend to not use Select class in these circumstances. Instead I use a locator which locates the option tag then use WebDriverWait to wait for the option to exist. Then I select it.
Reply all
Reply to author
Forward
0 new messages