【问题标题】:To select value from ajax dropdown elements having same classname using Selenium使用 Selenium 从具有相同类名的 ajax 下拉元素中选择值
【发布时间】:2015-08-06 19:08:04
【问题描述】:

我正在尝试从两个 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>

代码:

//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;
			}		
		}

【问题讨论】:

  • 您尝试过使用 xpath 吗?
  • @HelpingHands 不,我没有使用 xpath。
  • 那你应该试试。
  • 您共享的 HTML 不是实际的下拉列表,您可能正在使用一些插件/框架,它会从中生成下拉列表。
  • @HelpingHands:我尝试了 xpath,但出现“NoSuchElementException”错误。

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


【解决方案1】:

你可以使用这样的东西

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

【讨论】:

  • 我尝试了这两种解决方案,在使用 xpath 解决方案时得到“org.openqa.selenium.NoSuchElementException”。并为第二个解决方案获得“IndexOutOfBound”异常。请帮我解决这个问题。
  • @ChetanKumarT:用明确的等待试试。
  • @ManaliJ: 为 xpath 获取 "NoSuchElementException" " //div[@class='ac_results'][0] " 所以我认为显式等待不起作用。
  • @ChetanKumarT 好像你在那里做错了什么。您是否尝试打印 dropDowns 的大小,如果它为 0。那么您没有正确共享代码,如果它是 1,则重新检查第二个下拉列表是否可见/启用。如果是 2,那么它应该可以工作。
【解决方案2】:

我试过了,但没有得到网页元素列表

它的谷歌搜索选项卡测试脚本只搜索随机的东西:“跑” 我的代码:

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

【讨论】:

  • HTML 代码:HTML:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-10
  • 2016-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-28
  • 1970-01-01
相关资源
最近更新 更多