[英]Selecting dropdown in selenium webdriver, dropdown list of elements in ul of div
我是 Selenium Web Driver 技术的新手,以下是我的问题,无法找到解决方案,请任何人帮我找出 x 路径或 cs 选择器或名称。
下面是代码是页面中的下拉元素,
<div id="reportsManager_chzn" class="chzn-container chzn-container-single chzn-container-active" style="width: 220px;">
<a class="chzn-single" href="javascript:void(0)" tabindex="-1">
<span>Ajay Paul Chowdhury</span>
<div>
<b></b>
</div>
</a>
<div class="chzn-drop" style="left: -9000px; width: 218px; top: 24px;">
<div class="chzn-search">
<input type="text" autocomplete="off" style="width: 183px;">
</div>
<ul class="chzn-results">
<li id="reportsManager_chzn_o_1" class="active-result" style="">All</li>
<li id="reportsManager_chzn_o_2" class="active-result" style="">wer</li>
<li id="reportsManager_chzn_o_3" class="active-result" style="">sss</li>
<li id="reportsManager_chzn_o_4" class="active-result result-selected" style="">www</li>
<li id="reportsManager_chzn_o_5" class="active-result" style="">rrr</li>
<li id="reportsManager_chzn_o_6" class="active-result" style="">yyy</li>
<li id="reportsManager_chzn_o_7" class="active-result" style="">iii</li>
<li id="reportsManager_chzn_o_8" class="active-result" style="">ooo</li>
<li id="reportsManager_chzn_o_9" class="active-result" style="">ppp</li>
对于上面的代码,无法找到 xpath/cssselector/name 以便我可以从列表中动态选择任何下拉值
请任何人帮助我找出结果。
对于上面的代码我试过
1) 尝试 1
// by Xpath and it selects the list by list ID
Actions manager = new Actions(driver);
WebElement we1=driver.findElement(By.xpath("//[@id='reportsManager_chzn_o_23']"));
manager.moveToElement(we1).moveToElement(driver.findElement(By.xpath("//[@id='reportsManager_chzn_o_3']"))).click().build().perform();
Thread.sleep(3000);
上面的 Selenium 代码将选择下拉元素“ sss ”我需要按名称选择的代码,以便我可以参数化下拉列表
2)尝试2
driver.findElement(By.id("reportsManager_chzn")).findElement(By.cssSelector("chzn-single")).findElement(By.name("sss")).click();
显示错误信息
org.openqa.selenium.NoSuchElementException: 无法定位元素:{"method":"css selector","selector":"chzn-single"}
Selenium 测试用例失败
3) 尝试 3
由Select尝试
Select selectBox = new Select(driver.findElement(By.id("reportsManager_chzn")));
selectBox.selectByVisibleText("sss");
对于上面显示的错误消息找到 div 而不是 select
我也一路努力,终于到了
提前致谢
对参数化 xPath 使用以下方法:
public void selector(String whichItem)
{
String xPath_partial_1 ="//ul[@class='chzn-results']/li[reportsManager_chzn_o_"
String xPath_parameterized = whichItem; //This can be 1/2/3/4
String xPath_partial_2 = "]";
String final_xPath = xPath_partial_1 + xPath_parameterized + xPath_partial_2 ; //You can use this xPath to locate your element. Note that this xpath depends upon the number. Pass the correct number(i.e. 1 for selecting All, 2 for selecting wer and so on) to select the necessary item from the DD list
WebElement we1=driver.findElement(By.xpath("//[@id='reportsManager_chzn_o_23']")); //Could not locate this element in your codesnippet but assuming this is somewhere within the hierrarchy
Actions manager = new Actions(driver);manager.moveToElement(we1).moveToElement(driver.findElement(By.xpath(final_xPath))).click().build().perform(); //Just replaced the xPath with the String.
}
请注意,由于 DOM 中未使用 Select 标签,因此此处不能使用 Select Class。 让我知道它是否有帮助:)
我已经在这里发布了相同问题的答案。 对于 java 解决方案将是下一个(不确定 java 语法):
var dropDown = driver.findElement(By.Css(".chzn-results"));
//expand main dropDown menu before accessing to child elements
dropDown.Click()
var dropDownElements = dropDown.findElements(By.Css(".active-result"));
foreach(var dropDownElement in dropDownElements)
{
dropDownElement.Click();
}
//or your can access to element using index
dropDownElements[0].Click();
如果您想使用它的文本访问 dropDown 元素,更好的解决方案将属性添加到您的 li 元素(例如 li name="valueFromDropDown")
这将帮助你...
List <WebElement> allelements = driver.findElements(By.xpath("//*[@id='ui-select-choices-row-5-39']/a/span"));
for(int i=0;i<allelements.size();i++){
String text=allelements.get(i).getText();
if(text.equalsIgnoreCase("Test_Region1"))
{
allelements.get(i).click();
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.