Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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. Thanks for the answers. :)

share|improve this question
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
up vote 187 down vote accepted

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 documentend: 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

share|improve this answer
6  
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
3  
@WillSheppard XPathResult.FIRST_ORDERED_NODE_TYPE === 9 developer.mozilla.org/en-US/docs/… – yckart Nov 15 '13 at 21:57
    
it doesn't work Samsung Galaxy SIII default browser – Samuel.Cai Apr 3 '14 at 11:47
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. gist.github.com/Joopmicroop/10471650 – joopmicroop Apr 11 '14 at 14:15
    
Is it W3C standard? – Ciro Santilli 烏坎事件2016六四事件 法轮功 Jul 5 '14 at 10:08

In Chrome Dev Tools you can run the following:

$x("some xpath")
share|improve this answer
    
I've played around with this and it seems to work, but is there any documentation for this feature? I didn't find anything. – Eric Jan 30 at 19:40

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

share|improve this answer

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

share|improve this answer
5  
I'd like to note that {document.evaluate} does not work in IE. – Christopher Bales Jan 30 '13 at 22:30

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.