Unable to click Object despite verifyElementClickable returning true

You can make your XPath expressions better.

Context

You have this HTML fragment in your target web page:

    <div class="nav-bar-collapse collapse">
        <ul>
            <li>...</li>
            ...
        </ul>
    </div>

Your expression //div[@class='navbar-collapse collapse']/ul/li[1] will work well against this HTML.

Problem

However it is likely that sometime your HTML changes like this:

    <div class="collapse nav-bar-collapse">
        ...
    </div>

Please note the value of @class is different from the previous one. But CSS-semantically these are equivalent. Both are valid. Now you got a problem: your current expression //div[@class='navbar-collapse collapse']/ul/li[1] does not work against the changed HTML.

There can be even more annoying case:

    <div class="collapse nav-bar-collapse foo">
        ...
    </div>

How to support these cases by a single XPath expression?

Solution

Use XPath contains() function and logical operator and, rather than @class="alongstring" test.

//div[contains(@class,'navbar-collapse') and contains(@class, 'collapse')]/ul/li[1]
1 Like