简体   繁体   中英

Selecting dropdown in selenium webdriver, dropdown list of elements in ul of div

I am new to the Selenium Web Driver technology, below is my question and unable to find out the solution please any one help me to find out the by x path or by cs Selector or by name.

below is code is the drop down Element in the page,

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

for the above code unable to find out the xpath/cssselector/name for the so that i can Dynamically Select the any dropdown value from the List

Please any one help me to find out the result.

for above code i tried

1) try 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);

above Selenium code will Select the dropdown element ' sss ' i need code which select by name so that i can paramaterize the Dropdown List

2) try 2

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

showing the error message

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"chzn-single"}

and the Selenium testcase is failed

3) try 3

tried by Select

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

selectBox.selectByVisibleText("sss");

for the above shows the Error message found div instead of select

also i tried in all the way, finally i'm here

Thanks in advance

Use the following method with parameterized 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. 

}

Note that since Select tag was not used in the DOM, Select Class cannot be used here. Lemme know if it helps :)

I've already posted answer on same question here . For java solution will be next (not sure about java syntax):

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();

If you want access to dropDown element using it text, better solution add attribute to your li element (eg li name="valueFromDropDown")

This will help you...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM