I am trying to select a particular value from two Ajax drop down fields. But both the ajax value container is having same classname 'ac_results'. So only the value from the first ajax dropdown field is getting selected and its not identifying the second ajax dropdown value. Please help me with this regard. HTML code:
<div class="ac_results" style="display: none; position: absolute; width: 150px; top: 225px; left: 489.317px;">
<ul style="max-height: 180px; overflow: auto;">
<li class="ac_even"></li>
<li class="ac_odd"></li>
<li class="ac_even"></li>
<li class="ac_odd"></li>
<li class="ac_even"></li>
<li class="ac_odd"></li>
<li class="ac_even"></li>
</ul>
</div>
<div class="ac_results" style="display: none; position: absolute; width: 150px; top: 225px; left: 781.733px;">
<ul style="max-height: 180px; overflow: auto;">
<li class="ac_even"></li>
<li class="ac_odd"></li>
<li class="ac_even"></li>
<li class="ac_odd"></li>
</ul>
Code:
//get the from field
WebElement fromCity = driver.findElement(By.id("txtStationFrom"));
//Enter the value into the from city field
fromCity.sendKeys("ban");
//wait for some time
Thread.sleep(2000);
//get the ajax container having values
WebElement ajaxContainer1 = driver.findElement(By.className("ac_results"));
WebElement ajaxHolder1 = ajaxContainer1.findElement(By.tagName("ul"));
//Values in the container
List<WebElement> ajaxValues1 = ajaxHolder1.findElements(By.tagName("li"));
for (WebElement value1 : ajaxValues1) {
if(value1.getText().equals("BANGALORE CY JN- SBC")){
value1.click();
break;
}
}
//Get the to city field
WebElement toCity = driver.findElement(By.id("txtStationTo"));
//pass the value to the field
toCity.sendKeys("sub");
//Wait for some time
Thread.sleep(2000);
//get the container of the ajax toCity
WebElement ajaxContainer2 = driver.findElement(By.className("ac_results"));
WebElement ajaxHolder2 = ajaxContainer2.findElement(By.tagName("ul"));
List<WebElement> ajaxValues2 = ajaxHolder2.findElements(By.tagName("li"));
for (WebElement value2 : ajaxValues2) {
if(value2.getText().equals("SUBRAHMANYA ROAD- SBHR")){
value2.click();
break;
}
}