How to write Xpath to select a table from the example below + a good Xpath reference table available?

Some Xpath instructions are needed. I have something along the following lines in terms of HTML. How to extract the table following the first paragraph with an image?

<p align="center"> <img src="some_image.gif" alt="Some Title"> </p> <table width="500" border="1" class="textstyle" align="center" cellpadding="0" cellspacing="0"> <tr> <td colspan="4" align="center"> <b>Label</b> </td> </tr> <tr> <td align="center"> Text </td> <td align="center"> Text </td> <td align="center"> Text </td> <td align="center"> Text </td> </tr> </table> <blockquote> <p class="textstyle"> Text. </p> </blockquote> 

Obviously, I would like to see how to write xpath for this, but I would also like to sort out my options through some shorthand / cheatsheet about what options exist for xpath. I was considering using something like that:

 //table[preceding-sibling::p[contains(align(), "center") 

or maybe something that would allow me to tell ap that is focused on img in it, however I'm just not sure where to go to get a nice complete list and an actual entry that explains all the details of gory.

+4
source share
1 answer

Using

 (//p[img])[1]/following-sibling::table[1] 

This selects table , which is the first table - after the next sibling of the first p in the document, which has an img child.

I recommend using XPath Visualizer , a tool that has helped many thousands of people learn XPath in an interesting way .

+1
source

Source: https://habr.com/ru/post/1397370/


All Articles