Amit Saini
Amit Saini

Reputation: 1

List WebElement always selecting first element from the drop-down but i want to select 4th element

My list has approximate 190 items and items also getting traced one by one in for loop. I applied the condition to a specific text. however, debugger entering into condition but selecting always the first element from the drop-down. In mentioned code country "America" present on the 4th index of my drop-down.

List<WebElement> options=driver.findElements(By.xpath("//*[@id='Countryitems_popup']/div[1]/ul//li/span"));
for(int i=0;i<options.size();i++)
{
    WebElement select=options.get(i);
    String innerhtml=select.getAttribute("innerHTML");

    if(innerhtml.contentEquals("America"))
        {
            select.click();
            break;
        }
}

HTML is mentioned below:

<div id="Countryitems_popup" class="e-scroll e-js e-wid" tabindex="" style="height: auto; display: block;">
    <div class="h-con" style="height: 150px; width: 158.891px;">
        <ul class="H-Kl" role="listbox">
            <li data-value="001" id="004" role="option" unselectable="on" class="">
                <span class=" e-ddltxt">Country 1</span>
            </li>
            <li data-value="676" id="006" role="option" unselectable="on" class="">
                <span class=" e-ddltxt">Country 2</span>
            </li>
            <li data-value="765" id="009" role="option" unselectable="on" class="">
                <span class=" e-ddltxt">Country 3</span>
            </li>
            <li data-value="0067" id="065" role="option" unselectable="on" class="">
                <span class=" e-ddltxt">America</span>
            </li>
        </ul>
    </div>
</div>

Upvotes: 0

Views: 6102

Answers (3)

Navarasu
Navarasu

Reputation: 8479

You can achieve it with selector itself without loop. The xpath to find the list element with text 'America' will be as,

driver.findElement(By.xpath("//[@id='Countryitems_popup']/div[1]/ul//li[conatins(text(), 'America')]")).click()

Upvotes: 0

JeffC
JeffC

Reputation: 25654

Without seeing the page to test it, it's hard to know exactly what the issue is but if it's like other pages I've seen you will need to:

  1. Click the "dropdown" to open it (and make the options visible)
  2. Wait for the desired option to be clickable (indicating the list is open and visible)
  3. Click the desired option

My recommendation is to put this all in a method called selectCountry() (or something similar) and pass in the desired country name.

public void selectCountry(String countryName)
{
     // I'm assuming this is the container for the dropdown. Clicking it should open the dropdown
    driver.findElement(By.id("Countryitems_popup")).click();
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='Countryitems_popup']//span[.='" + countryName + "']))).click();
}

Upvotes: 0

cruisepandey
cruisepandey

Reputation: 29362

Use for each loop for more readability. Try with this code :

List<WebElement> options= driver.findElements(By.xpath("//[@id='Countryitems_popup']/div[1]/ul//li/span"));
            for(WebElement option : options) {
                if(option.getText().trim().equals("America")) {
                    option.click();
                    break;
                }
            }  

This should work, provided the xpath should be correct.

Upvotes: 2

Related Questions