1

I am trying to list all elements from the bootstrap dropdown and then select a certain value. However, it returns 0 values. Any suggestions will be greatly appreciated.

driver.findElement(By.id("imgSelectButton")).click();
Thread.sleep(3000);
List<WebElement> list = driver.findElements(By.xpath("//ul/li[@class='logoSelectOpt']//li"));
System.out.println(list.size());
for(int i=0; i<list.size(); i++){
    System.out.println(list.get(i).getText());
    if (list.get(i).getText().contains("History")){
        list.get(i).click();
        break;
    }
}

DOM:

<div class="logoSelect" style="z-index:1; top:878px;">==$0
 <ul>
  <li class="logoSelectOpt" id="12" onmouseover="jQuery(QWE01Title.activate(this);" onmouseout="jQuery(QWE01Title.deactivate(this);" onmousedown="jQuery(QWE01Title.selectItem(this);" logoColor="#FFF">Facts</li>==0
  <li class="logoSelectOpt" id="12" onmouseover="jQuery(QWE01Title.activate(this);" onmouseout="jQuery(QWE01Title.deactivate(this);" onmousedown="jQuery(QWE01Title.selectItem(this);" logoColor="#FFF">History</li>==0
  <li class="logoSelectOpt" id="12" onmouseover="jQuery(QWE01Title.activate(this);" onmouseout="jQuery(QWE01Title.deactivate(this);" onmousedown="jQuery(QWE01Title.selectItem(this);" logoColor="#FFF">Opinions</li>==0
  <li class="logoSelectOpt" id="12" onmouseover="jQuery(QWE01Title.activate(this);" onmouseout="jQuery(QWE01Title.deactivate(this);" onmousedown="jQuery(QWE01Title.selectItem(this);" logoColor="#FFF">Questions</li>==0
 </ul>
</div>

1 Answer 1

2

The elements are jQuery enabled element so to locate the elements you have to induce WebDriverWait for the visibilityOfAllElementsLocatedBy() and you can use either of the following Locator Strategies:

  • Using cssSelector:

    driver.findElement(By.id("imgSelectButton")).click();
    List<WebElement> list = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div.logoSelect > ul li.logoSelectOpt")));
    System.out.println(list.size());
    for(int i=0; i<list.size(); i++){
        System.out.println(list.get(i).getText());
        if (list.get(i).getText().contains("History")){
            list.get(i).click();
            break;
        }
    }
    
  • Using xpath:

    driver.findElement(By.id("imgSelectButton")).click();
    List<WebElement> list = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[@class='logoSelect']/ul//li[@class='logoSelectOpt']")));
    System.out.println(list.size());
    for(int i=0; i<list.size(); i++){
        System.out.println(list.get(i).getText());
        if (list.get(i).getText().contains("History")){
            list.get(i).click();
            break;
        }
    }
    
Sign up to request clarification or add additional context in comments.

3 Comments

DebanjanB thank you so much for your response. I've tried your methods but is throws a TimeoutExeption.
@AlexAlex TimeoutException is the outcome of failed ExpectedConditions. Debug your code through findElement() inconjunction with Thread.sleep(). If you are able to locate the element, update the question with the observations. Check the HTML and traverse upwards and observe if the element is within an <iframe> tag, then you have to switch to the desired <iframe> first before clicking the element. Check this discussion once
There was problem with my xpath. I removed the WebDriverWait, used your version of the xpath and it worked! Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.