gpt4 book ai didi

javascript - 在selenium webdriver中选择下拉列表,div的ul中元素的下拉列表

转载 作者:行者123 更新时间:2023-11-28 09:36:05 24 4
gpt4 key购买 nike

我是 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。让我知道它是否有帮助:)

关于javascript - 在selenium webdriver中选择下拉列表,div的ul中元素的下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25523817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com