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

时间:2014-08-27 09:41:38

标签: javascript jquery html css selenium

我是Selenium Web Driver技术的新手,下面是我的问题,无法找到解决方案,请任何人帮我找出x路径或cs Selector或名称。

下面是代码是页面中的下拉元素,

            <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代码将选择下拉元素&#39; sss &#39;我需要选择的代码 按名称,以便我可以对下拉列表进行参数化

2)尝试2

      driver.findElement(By.id("reportsManager_chzn")).findElement(By.cssSelector("chzn-single")).findElement(By.name("sss")).click();

显示错误消息

org.openqa.selenium.NoSuchElementException:无法找到元素:{&#34;方法&#34;:&#34; css选择器&#34;,&#34;选择器&#34; :&#34; chzn-single&#34;}

并且Selenium测试用例失败

3)尝试3

尝试选择

   Select selectBox = new Select(driver.findElement(By.id("reportsManager_chzn")));

selectBox.selectByVisibleText(&#34; SSS&#34);

以上显示错误信息找到div而不是选择

我也一直在尝试,最后我在这里

提前致谢

3 个答案:

答案 0 :(得分:0)

对参数化的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。 Lemme知道它是否有帮助:)

答案 1 :(得分:0)

我已经在同一个问题here上发布了答案。 对于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”)

答案 2 :(得分:0)

这会对你有帮助......

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