431

I am looking for something like:

getElementByXpath(//html[1]/body[1]/div[1]).innerHTML

I need to get the innerHTML of elements using JS (to use that in Selenium WebDriver/Java, since WebDriver can't find it itself), but how?

I could use ID attribute, but not all elements have ID attribute.

[FIXED]

I am using jsoup to get it done in Java. That works for my needs.

Cedric Druck
  • 1,032
  • 7
  • 20
pMan
  • 8,808
  • 11
  • 32
  • 35
  • 2
    Incidentally, the `html` and `body` selectors are superfluous since a DIV must be a descendent of BODY (immediate or deeper) and BODY must be a child of HTML, so provided there are no other DIV elements in the document, `//DIV[1]` should work (though I'm pretty rusty on XPath expressions). The DOM equivalent is `document.getElementsByTagName('div')[1]` (or maybe `0`). – RobG May 15 '12 at 10:29

11 Answers11

699

You can use document.evaluate:

Evaluates an XPath expression string and returns a result of the specified type if possible.

It is w3-standardized and whole documented: https://developer.mozilla.org/en-US/docs/Web/API/Document.evaluate

function getElementByXpath(path) {
  return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}

console.log( getElementByXpath("//html[1]/body[1]/div[1]") );
<div>foo</div>

https://gist.github.com/yckart/6351935

There's also a great introduction on mozilla developer network: https://developer.mozilla.org/en-US/docs/Introduction_to_using_XPath_in_JavaScript#document.evaluate


Alternative version, using XPathEvaluator:

function getElementByXPath(xpath) {
  return new XPathEvaluator()
    .createExpression(xpath)
    .evaluate(document, XPathResult.FIRST_ORDERED_NODE_TYPE)
    .singleNodeValue
}

console.log( getElementByXPath("//html[1]/body[1]/div[1]") );
<div>foo/bar</div>
William Desportes
  • 1,412
  • 1
  • 22
  • 31
yckart
  • 32,460
  • 9
  • 122
  • 129
  • 16
    What does the magic number 9 mean? It would be better to use a named constant here. – Will Sheppard Nov 15 '13 at 16:04
  • 11
    @WillSheppard `XPathResult.FIRST_ORDERED_NODE_TYPE === 9` https://developer.mozilla.org/en-US/docs/Introduction_to_using_XPath_in_JavaScript#First_Node – yckart Nov 15 '13 at 21:57
  • 2
    I wrote an getElementByXPath function that has support for IE but some basic xpath support for now. There is also a function getElementXpath in there and they work nicely together for what I needed. https://gist.github.com/Joopmicroop/10471650 – joopmicroop Apr 11 '14 at 14:15
  • @CiroSantilli Yes sir, it is: http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathEvaluator-evaluate – yckart Jul 05 '14 at 10:13
  • var xpathResult = document.evaluate( xpathExpression, contextNode, namespaceResolver, resultType, result ); – TechDog Oct 01 '15 at 17:02
  • How could this method be adapted in the context of a driver object? (I'm using WD.js for tests over Appium) – Alexander Fradiani Oct 04 '16 at 16:27
  • @AlexanderFradiani Have you tried `driver.findElement(webdriver.By.xpath())`? – yckart Oct 05 '16 at 14:39
  • How about xpath like this will not work. `"//*[text()[normalize-space() = 'HomePage' ]]"` – ji-ruh Jan 21 '17 at 18:17
  • How would we use this method to set the calendar value in Angular JS application – Ashok kumar Ganesan Nov 28 '19 at 11:01
  • This answer helped me greatly. In the end, I wanted to scroll a parent container of a ul, and here is a working scroll-down action, thanks to `evaluate` from this answer: `document.evaluate('//ul[@class="nav"]/..', document, null, 9).singleNodeValue.scrollTo(0, 1000)` – Neil Gaetano Lindberg Feb 11 '20 at 16:31
  • This seems to reload the page. – Sridhar Sarnobat Dec 14 '21 at 05:22
294

In Chrome Dev Tools you can run the following:

$x("some xpath")
Matt
  • 74,352
  • 26
  • 153
  • 180
Dmitry Semenyuk
  • 3,459
  • 1
  • 17
  • 12
35

To identify a WebElement using and you have to use the evaluate() method which evaluates an xpath expression and returns a result.


document.evaluate()

document.evaluate() returns an XPathResult based on an XPath expression and other given parameters.

The syntax is:

var xpathResult = document.evaluate(
  xpathExpression,
  contextNode,
  namespaceResolver,
  resultType,
  result
);

Where:

  • xpathExpression: The string representing the XPath to be evaluated.
  • contextNode: Specifies the context node for the query. Common practice is to pass document as the context node.
  • namespaceResolver: The function that will be passed any namespace prefixes and should return a string representing the namespace URI associated with that prefix. It will be used to resolve prefixes within the XPath itself, so that they can be matched with the document. null is common for HTML documents or when no namespace prefixes are used.
  • resultType: An integer that corresponds to the type of result XPathResult to return using named constant properties, such as XPathResult.ANY_TYPE, of the XPathResult constructor, which correspond to integers from 0 to 9.
  • result: An existing XPathResult to use for the results. null is the most common and will create a new XPathResult

Demonstration

As an example the Search Box within the Google Home Page which can be identified uniquely using the xpath as //*[@name='q'] can also be identified using the Console by the following command:

$x("//*[@name='q']")

Snapshot:

googlesearchbox_xpath

The same element can can also be identified using document.evaluate() and the xpath expression as follows:

document.evaluate("//*[@name='q']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

Snapshot:

document_evalute_xpath

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
30

For something like $x from chrome command line api (to select multiple elements) try:

var xpath = function(xpathToExecute){
  var result = [];
  var nodesSnapshot = document.evaluate(xpathToExecute, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
  for ( var i=0 ; i < nodesSnapshot.snapshotLength; i++ ){
    result.push( nodesSnapshot.snapshotItem(i) );
  }
  return result;
}

This MDN overview helped: https://developer.mozilla.org/en-US/docs/Introduction_to_using_XPath_in_JavaScript

Jay
  • 3,857
  • 2
  • 23
  • 25
14

Although many browsers have $x(xPath) as a console built-in, here's an aggregation of the useful-but-hardcoded snippets from Introduction to using XPath in JavaScript ready for use in scripts:

Snapshot

This gives a one-off snapshot of the xpath result set. Data may be stale after DOM mutations.

const $x = xp => {
  const snapshot = document.evaluate(
    xp, document, null, 
    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null
  );
  return [...Array(snapshot.snapshotLength)]
    .map((_, i) => snapshot.snapshotItem(i))
  ;
};

console.log($x('//h2[contains(., "foo")]'));
<h2>foo</h2>
<h2>foobar</h2>
<h2>bar</h2>

First ordered node

const $xOne = xp => 
  document.evaluate(
    xp, document, null,
    XPathResult.FIRST_ORDERED_NODE_TYPE, null
  ).singleNodeValue
;

console.log($xOne('//h2[contains(., "foo")]'));
<h2>foo</h2>
<h2>foobar</h2>
<h2>bar</h2>

Iterator

Note however, that if the document is mutated (the document tree is modified) between iterations that will invalidate the iteration and the invalidIteratorState property of XPathResult is set to true, and a NS_ERROR_DOM_INVALID_STATE_ERR exception is thrown.

function *$xIter(xp) {
  const iter = document.evaluate(
    xp, document, null, 
    XPathResult.ORDERED_NODE_ITERATOR_TYPE, null
  );

  for (;;) {
    const node = iter.iterateNext();
    
    if (!node) {
      break;
    }
    
    yield node;
  }
}

// dump to array
console.log([...$xIter('//h2[contains(., "foo")]')]);

// return next item from generator
const xpGen = $xIter('//h2[text()="foo"]');
console.log(xpGen.next().value);
<h2>foo</h2>
<h2>foobar</h2>
<h2>bar</h2>
ggorlen
  • 44,755
  • 7
  • 76
  • 106
11

You can use javascript's document.evaluate to run an XPath expression on the DOM. I think it's supported in one way or another in browsers back to IE 6.

MDN: https://developer.mozilla.org/en-US/docs/Web/API/Document/evaluate

IE supports selectNodes instead.

MSDN: https://msdn.microsoft.com/en-us/library/ms754523(v=vs.85).aspx

RobG
  • 142,382
  • 31
  • 172
  • 209
7

To direct to the point , you can easily use xapth .The exact and simple way to do this using the below code . Kindly try and provide feedback .Thank you .

JavascriptExecutor js = (JavascriptExecutor) driver;

    //To click an element 
    WebElement element=driver.findElement(By.xpath(Xpath));
    js.executeScript(("arguments[0].click();", element);

    //To gettext

    String theTextIWant = (String) js.executeScript("return arguments[0].value;",driver.findElement(By.xpath("//input[@id='display-name']")));

Further readings - https://medium.com/@smeesheady/webdriver-javascriptexecutor-interact-with-elements-and-open-and-handle-multiple-tabs-and-get-url-dcfda49bfa0f

Sameera De Silva
  • 1,722
  • 1
  • 22
  • 41
2

Assuming your objective is to develop and test your xpath queries for screen maps. Then either use Chrome's developer tools. This allows you to run the xpath query to show the matches. Or in Firefox >9 you can do the same thing with the Web Developer Tools console. In earlier version use x-path-finder or Firebug.

Martin Spamer
  • 5,437
  • 28
  • 44
2
public class JSElementLocator {

    @Test
    public void locateElement() throws InterruptedException{
        WebDriver driver = WebDriverProducerFactory.getWebDriver("firefox");

        driver.get("https://www.google.co.in/");


        WebElement searchbox = null;

        Thread.sleep(1000);
        searchbox = (WebElement) (((JavascriptExecutor) driver).executeScript("return document.getElementById('lst-ib');", searchbox));
        searchbox.sendKeys("hello");
    }
}

Make sure you are using the right locator for it.

display-name-is-missing
  • 4,424
  • 5
  • 28
  • 41
Prerit Jain
  • 200
  • 1
  • 5
  • 1
    Hi Prerit, the question was to select by an element based on its xpath. The solution you have provided is to select it by the id. :) – Gaurav Thantry Jun 08 '18 at 18:56
2
**Different way to Find Element:**

IEDriver.findElement(By.id("id"));
IEDriver.findElement(By.linkText("linkText"));
IEDriver.findElement(By.xpath("xpath"));

IEDriver.findElement(By.xpath(".//*[@id='id']"));
IEDriver.findElement(By.xpath("//button[contains(.,'button name')]"));
IEDriver.findElement(By.xpath("//a[contains(.,'text name')]"));
IEDriver.findElement(By.xpath("//label[contains(.,'label name')]"));

IEDriver.findElement(By.xpath("//*[contains(text(), 'your text')]");

Check Case Sensitive:
IEDriver.findElement(By.xpath("//*[contains(lower-case(text()),'your text')]");

For exact match: 
IEDriver.findElement(By.xpath("//button[text()='your text']");

**Find NG-Element:**

Xpath == //td[contains(@ng-show,'childsegment.AddLocation')]
CssSelector == .sprite.icon-cancel
Alok Patel
  • 54
  • 2
1
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

System.setProperty("webdriver.chrome.driver", "path of your chrome exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.google.com");

            driver.findElement(By.xpath(".//*[@id='UserName']")).clear();
            driver.findElement(By.xpath(".//*[@id='UserName']")).sendKeys(Email);