How to execute xpaths without using Java code | Selenium Forum
M
Posted on 23/10/2015
How to execute xpath without using Java code
Using plain xpath.

M
Replied on 23/10/2015

execute what?


M
Replied on 23/10/2015

Suppose there's an xpath,
This has been asked in an interview that how can we use any xpath without using any Java code.


M
Replied on 23/10/2015

You can use javascript's document.evaluate to run an XPath expression on the DOM. It should looks like as per below code -

var evaluator = new XPathEvaluator();

//get first div
var result = evaluator.evaluate("//div", document.documentElement, null,
XPathResult.FIRST_ORDERED_NODE_TYPE, null);

This method accepts five arguments: the XPath query string, the node from which the query should begin, a namespace resolver (discussed later), the type of result to return, and an optional result object onto which the new results should be added. The last argument is rarely used since the result is also returned as the value of evaluate().


M
Replied on 24/10/2015

yes you can use java script executor.