Not directly from WebDriver, but you can fake it if you really need to:
public String getElementXPath(WebDriver driver, WebElement element) {
return (String)((JavascriptExecutor)driver).executeScript("gPt=function(c){if(c.id!==''){return'id(\"'+c.id+'\")'}if(c===document.body){return c.tagName}var a=0;var e=c.parentNode.childNodes;for(var b=0;b<e.length;b++){var d=e[b];if(d===c){return gPt(c.parentNode)+'/'+c.tagName+'['+(a+1)+']'}if(d.nodeType===1&&d.tagName===c.tagName){a++}}};return gPt(arguments[0]).toLowerCase();", element);
}
Javascript is from this post , reduced to fit one line. This may not be perfect, but it can give you an idea of ββwhere to go. Most drivers implement the interface JavascriptExecutorand have the ability to run Javascript in a browser. executeScriptcan return any primitive JavaScript type, HTML element or non-nested list of any of the previous ones.
xpath , , xpaths . , xpath ( IE cough), .