purer
purer

Reputation: 477

xpath - pick based on another child of parent node

I have page that looks something like this:

<div>
    <div>
        <div>
            <span class="span class one">
                some text
            </span>
        </div>
    </div>
    <div>
        <div>
            <span class="span class two">
                span i want to pick
            </span>
        </div>
    </div>
</div>

I want to pick <span class="span class two"> by text thats in <span class="span class one">. I am not sure if it is even possible. Number of elements is not same in each tree part.

Upvotes: 1

Views: 2160

Answers (5)

Jainish Kapadia
Jainish Kapadia

Reputation: 2611

Try this way, as you were mentioned that you want to create xpath along with span class one

//span[text()= 'some text']/following::span[@class='span class two']

Explanation of xpath:- Use text method along with <span> tag and move ahead with another <span> tag using following keyword.

Upvotes: 0

becixb
becixb

Reputation: 373

I might've understood it differently but I'll try to give out a different answer: //span[contains(text(),(//span[@class='span class one']/text())) and not(@class='span class one')]

which means:

//span[contains(text(), - you're looking for a span element that contains a certain text

(//span[@class='span class one']/text()))- that text is whatever is the text in span class one

and not(@class='span class one')] - but the span element should not be span class one

of course you can replace text() with a different property such as class or name or whatever... e.g. //span[contains(@class,(//span[@class='span class one']/text()))]

Upvotes: 0

NarendraR
NarendraR

Reputation: 7708

Following could be the alternative answer -

//span[normalize-space(text())='some text']/../../following-sibling::div//span

Explanation :-

//span[normalize-space(text())='some text'] is used to find the span tag with required details

/../.. will move to parent element of context node

/following-sibling::div//span will locate the span tag which in sibling element of parent div

Upvotes: 1

宏杰李
宏杰李

Reputation: 12168

//span[contains(., "some text")]/following::span

out:

Element='<span class="span class two">
                span i want to pick
            </span>'

Upvotes: 0

PhillyNJ
PhillyNJ

Reputation: 3901

You can select the element by the value of the class attribute with:

//span[@class='span class two']

Upvotes: 0

Related Questions