Scott-David Jones
Scott-David Jones

Reputation: 412

How to click a link using XPath and Selenium IDE

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?

Upvotes: 0

Views: 15233

Answers (1)

Aleh Douhi
Aleh Douhi

Reputation: 1978

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

Upvotes: 3

Related Questions