Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPATH Query: How to get two elements?

Tags:

xpath

My HTML code is:

<table>
<tr>
<td class="data1"><p>1</td></td>
<td class="data1"><p>2</td></td>
<td class="data1"><p>3</td></td>
<td class="data1"><p>4</td></td>
</tr>
<tr>
<td class="data1"><p>5</td></td>
<td class="data1"><p>6</td></td>
<td class="data1"><p>7</td></td>
<td class="data1"><p>8</td></td>
</tr>
</table>

My query is:

xpath='//tr//td[@class="data1"][4]/p'

The results is:

<p>4</p>
<p>8</p>

The results is correct! but, if I want to get example:

<p>3</p> <p>4</p>

<p>7</p> <p>8</p>

So

[3]/p and [4]/p

How to get these two elements each <tr> ?

Thank you so much!

like image 556
Damiano Avatar asked May 08 '10 10:05

Damiano


People also ask

How do I give two properties in XPath?

The syntax for locating elements through XPath- Multiple Attribute can be written as: //<HTML tag>[@attribute_name1='attribute_value1'][@attribute_name2='attribute_value2]

How do I select the second element in XPath?

//div[@class='content'][2] means: Select all elements called div from anywhere in the document, but only the ones that have a class attribute whose value is equal to "content". Of those selected nodes, only keep those which are the second div[@class = 'content'] element of their parent.

How do you combine two XPath expressions?

selects the union of all nodes selected by expr1 and all nodes selected by expr2. The | character denotes the XPath union operator. You can use the union operator in any case when you want the union of the nodes selected by several XPath expressions to be returned.

Can multiple elements have same XPath?

For example if both text fields have //input[@id='something'] then you can edit the first field xpath as (//input[@id='something'])[1] and the second field's xpath as (//input[@id='something'])[2] in object repository.


1 Answers

I think what you might be looking for is something along the lines of

[position() > 2]

which retrieves all elements after the first two.

like image 169
Lars Andren Avatar answered Jan 03 '23 00:01

Lars Andren