使用Selenium从具有相同类名的ajax下拉元素中选择值

时间:2015-05-25 10:16:49

标签: java ajax selenium drop-down-menu selenium-webdriver

我正在尝试从两个Ajax下拉字段中选择一个特定值。但是ajax值容器都具有相同的类名' ac_results'。因此,只有第一个ajax下拉字段的值被选中,而且它没有识别第二个ajax下拉值。请帮我这方面。 HTML代码:



 <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>
&#13;
&#13;
&#13;

代码:

&#13;
&#13;
//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;
			}		
		}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

你可以使用这样的东西

//div[@class='ac_results'][0] // For first Drop Down
//div[@class='ac_results'][1] // For second Drop Down

or
List<WebElement> dropDowns = driver.findElements(By.className('ac_results'));
WebElement dropDownOne = dropDowns.get(0); // perform further action using this WebElement
WebElement dropDownTwo = dropDowns.get(1); // Second drop down

答案 1 :(得分:0)

我试过,但没有获取网页元素列表

其谷歌搜索选项卡测试脚本,只搜索随机的东西:&#34;运行&#34; 我的代码:

@Test
enter code herepublic void checkDropDownofSearchBox(){
    int i = 0;
    WebElement dropDown = driver.findElement(By.className("sbdd_a"));
    WebElement ajaxHolder1 = dropDown.findElement(By.className("sbdd_b"));

    List<WebElement> ajaxValues1 = ajaxHolder1.findElements(By.tagName("li"));

    for (WebElement value1 : ajaxValues1) {
        System.out.println(value1.getText());
        i++;
    }
    System.out.println(i);
}