Automating jQuery based bootstrap dropdown using Selenium and Java(使用 Selenium 和 Java 自动化基于 jQuery 的引导下拉菜单)
问题描述
我正在尝试从引导下拉列表中列出所有元素,然后选择某个值.但是,它返回 0 值.任何建议将不胜感激.
I am trying to list all elements from the bootstrap dropdown and then select a certain value. However, it returns 0 values. Any suggestions will be greatly appreciated.
driver.findElement(By.id("imgSelectButton")).click();
Thread.sleep(3000);
List<WebElement> list = driver.findElements(By.xpath("//ul/li[@class='logoSelectOpt']//li"));
System.out.println(list.size());
for(int i=0; i<list.size(); i++){
System.out.println(list.get(i).getText());
if (list.get(i).getText().contains("History")){
list.get(i).click();
break;
}
}
DOM:
<div class="logoSelect" style="z-index:1; top:878px;">==$0
<ul>
<li class="logoSelectOpt" id="12" onmouseover="jQuery(QWE01Title.activate(this);" onmouseout="jQuery(QWE01Title.deactivate(this);" onmousedown="jQuery(QWE01Title.selectItem(this);" logoColor="#FFF">Facts</li>==0
<li class="logoSelectOpt" id="12" onmouseover="jQuery(QWE01Title.activate(this);" onmouseout="jQuery(QWE01Title.deactivate(this);" onmousedown="jQuery(QWE01Title.selectItem(this);" logoColor="#FFF">History</li>==0
<li class="logoSelectOpt" id="12" onmouseover="jQuery(QWE01Title.activate(this);" onmouseout="jQuery(QWE01Title.deactivate(this);" onmousedown="jQuery(QWE01Title.selectItem(this);" logoColor="#FFF">Opinions</li>==0
<li class="logoSelectOpt" id="12" onmouseover="jQuery(QWE01Title.activate(this);" onmouseout="jQuery(QWE01Title.deactivate(this);" onmousedown="jQuery(QWE01Title.selectItem(this);" logoColor="#FFF">Questions</li>==0
</ul>
</div>
推荐答案
元素是 jQuery 启用元素所以找到您必须为 visibilityOfAllElementsLocatedBy()
诱导 WebDriverWait 的元素,您可以使用以下任一 定位器策略:
The elements are jQuery enabled element so to locate the elements you have to induce WebDriverWait for the visibilityOfAllElementsLocatedBy()
and you can use either of the following Locator Strategies:
使用cssSelector:
driver.findElement(By.id("imgSelectButton")).click();
List<WebElement> list = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div.logoSelect > ul li.logoSelectOpt")));
System.out.println(list.size());
for(int i=0; i<list.size(); i++){
System.out.println(list.get(i).getText());
if (list.get(i).getText().contains("History")){
list.get(i).click();
break;
}
}
使用xpath:
driver.findElement(By.id("imgSelectButton")).click();
List<WebElement> list = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[@class='logoSelect']/ul//li[@class='logoSelectOpt']")));
System.out.println(list.size());
for(int i=0; i<list.size(); i++){
System.out.println(list.get(i).getText());
if (list.get(i).getText().contains("History")){
list.get(i).click();
break;
}
}
这篇关于使用 Selenium 和 Java 自动化基于 jQuery 的引导下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Selenium 和 Java 自动化基于 jQuery 的引导下拉


基础教程推荐
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01