3
votes

I feel that I do not understand XPath as well as I should. I have a java-based webdriver that opens a webpage, returns one element, and then continues. I would like to speed this up and only open the webpage already pointing to the element. Is this possible?

For example, I open up the website at www.example.com and return element wherein the xpath=/html/body/div[3]/table/tbody/tr[2]/td/table/tbody/tr[2]/td[3] .

I want to do that in one step.

I want to just be able to query www.example.com/html/body/div[3]/table/tbody/tr[2]/td/table/tbody/tr[2]/td[3]

How would I go about implementing something like this in my program? Would it even be faster?

Thank you in advance.

3
This is difficult to understand, and more so difficult to understand what your intended solution is trying to show. Give some definitive examples. - Arran

3 Answers

1
votes

I take it from your question that you want to start a WebDriver and have it already pointed to www.example.com, so that you can just do you query in one step without first going to the page.

The answer is, you can't. You have to settle for two steps:

// Go to page
driver.get("http://www.example.com");
// Find element
driver.findElement(By.xpath("/html/body/div[3]/table/tbody/tr[2]/td/table/tbody/tr[2]/td[3]"));

You can't just query a Web Page for an element without actually going to the page first and loading it in your browser. So no, you can't skip a step like you want to, to save time and speed up your application.

1
votes

XPath, the XML Path Language, is a query language for selecting nodes from an XML document.

With XPath you can't make HTTP requests and get HTTP responses, it is just a language to select XML nodes from an XML document.

0
votes

I think these answers may need a little bit of an update, though, I want to note, that my answer is going to be purley XPath related. I have no knowledge about Java WebDrivers.

You can't do this with XPath 1.0. But you can do this with XPath, starting from version 2.0 (XPath and XQuery Functions and Operators 3.1 is the official documentation of the XPath functions for XPath 3,1, which, at this writing, is the current implementation. You can also find a, much more accessible, reference here.). The function, to do so is:

fn:doc($uri as xs:string?) as document-node()?

which would return the document as XML document node. So, your above query would be named:

doc("www.example.com")/html/body/div[3]/table/tbody/tr[2]/td/table/tbody/tr[2]/td[3]

However, this does not result in an XML fragment being produced. Your client still would download the full HTML document and then, locally, extract the requested path.

To query an explicit XPath from a remote XML resource would require a few steps:

  • a change in the URI format specification [RFC 3986], that introduces an escape character, that marks an XPath expression within the URI. A single character, that tells the URI receiver: "all text after this character is an XPath expression". There was an interesting article regarding this, "Can a URL contain an XPath?", by Rick Jelliffe of Schematron fame on O'Reilly, but O'Reilly seems to have killed off those articles, the old links lead to their main landing page.
  • web servers, that could evaluate this URI and serve back the requested XML fragment (instead the whole document). This also would mean, that in your case, the fn:doc() function would not be needed (and the wrong tool for the job anyway, since, as stated, it always returns the full document).
  • last, but not least, a client, that can deal with XML fragments.

Sadly, these developments have been abandoned with the introduction of HTML5.