1

I’m new to both XPath and Selenium. I have a need to be able to click a link with Selenium based on whether or not a span is as a certain text. The example code is:

<div>
    <span>Fri</span>
    <ul>
        <li>
            <a href=#></a>
        </li>
    </ul>
</div>

<div>
    <span>Sat</span>
    <ul>
        <li>
            <a href=#></a>
        </li>
    </ul>
</div>

My XPath expression is //*[text()[contains(.,'Fri')]] which finds the correct span. Now I thought I could use //*[text()[contains(.,'Fri')]]../ul/li/a, but that doesn't work.

How can I do it?

0

1 Answer 1

4

The Selenium IDE command will be:

command: click
target : //span[contains(text(),'Fri')]/following::a

From here:

following:: All nodes that are after the context node in the tree, excluding any descendants, attribute nodes, and namespace nodes.

Notice, that you may not write [1] at the end of the XPath expression to select the first node from all occurrences as the Selenium IDE does it by default.

As you use the IDE you can also use easier to read CSS selector:

command: click
target : css=span:contains('Fri')+ul a

As for your initial XPath expression, you missed one slash before ../:

//*[text()[contains(.,'Fri')]]/../ul/li/a
Sign up to request clarification or add additional context in comments.

1 Comment

excellent, thanks for the break down, using your advice i was able to get it to work with the following: //span[text()[contains(.,'10')]]/../ul/li/a/ however for future items i think i'll use the css selector as it does indeed look easier!

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.