Selecting from div class dropdown - Selenium

后端 未结 3 742
悲哀的现实
悲哀的现实 2021-01-22 09:34

I\'m trying to select an option from a drop-down that doesnt populate until the locator has been clicked. This is what I see in Firebug:

div class=\"selectize-i         


        
相关标签:
3条回答
  • 2021-01-22 10:21

    This isn't a normal drop-down select menu. Hence, using Select won't work in this case. Without seeing the complete site, I'm not sure what exactly must be done to select it.

    But try simply clicking on the div element when the options in the dropdown are visible.

    //I'm assuming that this will display the dropdown list
    driver.findElement(byAgentCodes).click(); 
    
    driver.findElement(By.xpath("//div[@data-value='523-23-20275']"));
    
    0 讨论(0)
  • 2021-01-22 10:24

    Here, Select class wouldn't work if there is no select tag in the UI, you need to click on the main div, and then you need to click on any of the div having multiple options, it will first click on the drop down and then click on the specific element from the list, below code will hopefully work for you....

    1)firstly you need to click on this div by finding it through any of the available methods like by id, xpath, css selector, driver.findElement(byAgentCodes).click(); clicking on this will open a dropdown list

    2) repeat the same above point 1 for clicking the any of list items in dropdown

    523-23-20275

    That will work.

    0 讨论(0)
  • Follow the below steps to select an item under div tag:

    You have to use collections object to store all child elements which are stored under same tag. For Ex if you have below HTML structure:

    <div id="Year">
        <div class="abc">
             <ul class = "xyz">
                 <li id=1>2000</li>
                 <li id=2>2001</li>
                 <li id=3>2002</li>
                 <li id=4>2003</li>
                 <li id=5>2004</li>
             </ul>
        </div>
    </div>
    

    Write below selenium code:

    List<Webelement> lst = driver.findElements(By.xpath(<locator of child elements>));
    
    //In this case it is //div[@id='Year']/div/ul/li
    

    System will store all child elements in the list then you can select any element by index method using

    lst.get(<index value>).click();
    

    if you do not want to find using index but text use Iterator interface to find the element from the collection then click on that element:

    Iterator<Webelement> it = lst.iterator();
    while (it.hasNext()) {
        WebElement wb  = it.next();
        if(wb.getText().equals(<Text to find in double quotes>)) {
            wb.click();
            break;
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题