user3659762
user3659762

Reputation: 21

Xpath to find the first child if a sibling contains certain text

I need to find the XPATH for <li>Test1</li> in this case if the list contains <li>Sample</li>.

Example of Tags:

<ul>
        <li>Test1</li>
        <li>Test2</li>
        <li>Sample</li>
</ul>

<ul>
        <li>TestA</li>
        <li>TestB</li>
        <li>TestC</li>
</ul>

There can be multiple <ul> tags

Have been able to find whether li contains Sample using below query

//ul/li[contains(.,'Sample')]

Any thoughts on how to find the xpath of <li>Test1</li>?

Upvotes: 2

Views: 4391

Answers (1)

Jeff Mercado
Jeff Mercado

Reputation: 134521

You can use this xpath:

//ul[contains(li,'Sample')]/li[1]

The idea is to first find the ul element that has a li element that contains 'Sample'. Then from that, select the first li in that node set.

Upvotes: 2

Related Questions