1

I am trying to access the elements of an unordered list, but it seems to be a problem. Here is my function code,

public void getLinks() throws Exception
    {
        new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.id("create-study")));
        driver.findElement(By.id("create-study")).click();
        Thread.sleep(600);
        List<WebElement> studyLinks = driver.findElements(By.cssSelector("#warren-nav > div.collapse.navbar-collapse.navbar-ex1-collapse > ul > li.dropdown.open > ul"));
        int sSize = studyLinks.size();
        System.out.println("No of links: " + sSize);
    }

I am calling the above mentioned function in my test case, in another class (nothing wrong with that class, so not posting that here).

The HTML of the list is:

<li class="dropdown">
    <a class="dropdown-toggle" data-toggle="dropdown" href="#" id="create-study">
        <i class="fa fa-plus-circle warren-nav-icon"></i>Create
    </a>
    <ul class="dropdown-menu create-dropdown">
        <li data-study-type="event">
            <a href="/finance/warren/studies/new/kensho.event" class="create-study-link event" target="_self">
                <i class="fa fa-calendar" title="event"></i> Event Analysis
            </a>
        </li>
        <li data-study-type="cyclical">
            <a href="/finance/warren/studies/new/kensho.cyclical" class="create-study-link cyclical" target="_self">
                <i class="fa fa-retweet" title="cyclical"></i> Cyclical Analysis
            </a>
        </li>
        <li data-study-type="conditional">
            <a href="/finance/warren/studies/new/kensho.conditional" class="create-study-link conditional" target="_self">
                <i class="fa fa-random" title="conditional"></i> Conditional Analysis
            </a>
        </li>
        <li data-study-type="multi_condition">
            <a href="/finance/warren/studies/new/kensho.multi_condition" class="create-study-link multi_condition" target="_self">
                <i class="fa fa-random" title="multi_condition"></i> Multiple Conditions Analysis
            </a>
        </li>
        <li data-study-type="relative">
            <a href="/finance/warren/studies/new/kensho.relative" class="create-study-link relative" target="_self">
                <i class="fa fa-bar-chart-o" title="relative"></i> Relative Analysis
            </a>
        </li>
        <li data-study-type="relative_multiple">
            <a href="/finance/warren/studies/new/kensho.relative_multiple" class="create-study-link relative_multiple" target="_self">
                <i class="fa fa-bar-chart-o" title="relative_multiple"></i> Relative Analysis: Multiple Date Ranges
            </a>
        </li>
        <li data-study-type="regime_change">
            <a href="/finance/warren/studies/new/kensho.regime_change" class="create-study-link regime_change" target="_self">
                <i class="fa fa-globe" title="regime_change"></i> Global Scenario Analysis
            </a>
        </li>
        <li data-study-type="consensus_analysis">
            <a href="/finance/warren/studies/new/kensho.consensus_analysis" class="create-study-link consensus_analysis" target="_self">
                <i class="fa fa-puzzle-piece" title="consensus_analysis"></i> Economic Consensus/Surprise Analysis
            </a>
        </li>
        <li data-study-type="trigger">
            <a href="/finance/warren/studies/new/kensho.trigger" class="create-study-link trigger" target="_self">
                <i class="fa fa-random" title="trigger"></i> Trigger Analysis
            </a>
        </li>
        <li data-study-type="earnings_analysis">
            <a href="/finance/warren/studies/new/kensho.earnings_analysis" class="create-study-link earnings_analysis" target="_self">
                <i class="fa fa-dot-circle-o" title="earnings_analysis"></i> Earnings Consensus/Surprise Analysis
            </a>
        </li>
        <li data-study-type="price_movement_analysis">
            <a href="/finance/warren/studies/new/kensho.price_movement_analysis" class="create-study-link price_movement_analysis" target="_self">
                <i class="fa fa-line-chart" title="price_movement_analysis"></i> Price Movement Trigger Analysis
            </a>
        </li>
    </ul>
</li>

The inspection of the HTML code is following, the first line represents a button clicking which displays the dropdown list, the next line represents the div container, that contains the elements of the list. When I run my test case, the size I am getting is 1, whereas the number of links are 11.

Can anyone tell me what I'm doing wrong here. Thanks

3
  • Side note... you do this great wait.until() on the first line of the function and then you take a left turn and do a Thread.sleep() two lines later. :( Why not use another wait.until() for whatever changes after the click? Thread.sleeps() should be avoided for a couple reasons... 1) what if 600ms isn't enough time sometimes? Then your test fails anyway. 2) what if 600ms is too much time? Your test will sit and wait even though the page is ready. Use a proper wait and your test will be faster and less prone to break intermittently.
    – JeffC
    Sep 9, 2015 at 4:45
  • When you post HTML, please use an HTML formatter like jsbeautifier.org. It makes it much easier to read and is very simple and quick to use.
    – JeffC
    Sep 9, 2015 at 4:46
  • @JeffC I used Thread.sleep() because I want my execution to wait for exactly 600 ms at that point, even though the element is visible but I need it to wait for this time. If there is any other way to accomplish that, let me know.
    – Uziii
    Sep 9, 2015 at 6:16

2 Answers 2

1

I suppose you stopped too early, since you wanted to count the a-elements, you also need to "ask" for them.

I also cut down the selectors to the interesting part:

List<WebElement> studyLinks = driver.findElements(By.cssSelector(" li.dropdown > ul > a"));

basically you need to add "> a" to your css-query.

What I don't understand in your css-query that you're trying to find an li element with classes "dropdown" & "open" where your html code only shows an li with the "dropdown" class but not one with "open" class. But maybe you didn't post all of your code.

0

If you don't mind using xpath:

List<WebElement> studyLinks = driver.findElements(By.xpath("//*[contains(@href, 'finance')]"));

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.