tags:

views:

542

answers:

5

Hi All,

How to create XPATH for a HTML DOM element? for example, "/HTML/BODY/DIV[1]/TABLE[1]/TR[2]/TD[1]/INPUT".

Given an DOM element how to get this XPATH string? Any ideas?

Thanks,

Dattebayo.

+1  A: 

As I recall, the xpath checker extension to firefox gives you a point-and-click interface for getting the xpath to DOM elements in a HTML document.

Steen
A: 

After a lot of struggle I found a way to do so.

Along with the DOM path also use the SourceIndex of each node. Like "/Html:1/Body:2/Div:5/Input:6"

But again, 1. This might not work in case of dynamic page (ajax to modify the content). 2. This might not be unique accross browsers since the sourceIndex might vary accross browsers based on the Browser Rendering Engine arranges the nodes. (not sure of this yet though, just a thought).

dattebayo
A: 

In Mozilla an xpath generator component was implemented although it never made it to default builds.

You can find the tests in the "final patch" attached to the bug I linked to to see how it can be used. You can also look up its implementation, might be helpful.

Nickolay
+1  A: 

You can create a new domdocument and then import the node element

$DD= new DOMDocument('1.0', 'utf-8');
$DD->loadXML( "<html></html>" );
$DD->documentElement->appendChild($DD->importNode($DE,true));

then you can use xpath insithe the domelement:

$xpathe=new DOMXPath($DD);
Sergi Mayordomo
A: 

Here is a chrome extension that might help you (ChromyQlip)

https://chrome.google.com/extensions/detail/bkmllkjbfbeephbldeflbnpclgfbjfmn

Markandey Singh