How to get the xpath of a WebElement?

7,764 views
Skip to first unread message

chsonnu

unread,
Jul 24, 2011, 3:18:05 AM7/24/11
to Selenium Users
After finding an element by xpath wildcards, is there a way to extract
the specific xpath from the returning WebElement?

Tarun Bhadauria

unread,
Jul 24, 2011, 10:59:55 AM7/24/11
to seleniu...@googlegroups.com
A lil confusing, can you post an example?

chsonnu

unread,
Jul 24, 2011, 2:30:55 PM7/24/11
to Selenium Users
Sure.

# Find an element by xpath

foo = driver.FindElement(By.xpath("//div[text()='bar'"));

Now how would I extract the full xpath from foo? I'm talking the full
thing like "/html/body/div/div/div[2]/[div/div".

micsky

unread,
Jul 24, 2011, 5:17:30 PM7/24/11
to Selenium Users
Here's what I use:

protected String getXPath() {
String jscript = "function getPathTo(node) {" +
" var stack = [];" +
" while(node.parentNode !== null) {" +
" stack.unshift(node.tagName);" +
" node = node.parentNode;" +
" }" +
" return stack.join('/');" +
"}" +
"return getPathTo(arguments[0]);";
return (String) driver.executeScript(jscript, webElement);
}


Cheers,
-Mike

chsonnu

unread,
Jul 24, 2011, 5:49:51 PM7/24/11
to Selenium Users
Hi Mike, thanks a lot. I totally forgot that native javascript was
always available.

Mark Collin

unread,
Jul 25, 2011, 4:11:50 AM7/25/11
to seleniu...@googlegroups.com
My first question is why? What are you trying to achieve?

--
You received this message because you are subscribed to the Google Groups
"Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to
selenium-user...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/selenium-users?hl=en.


--
This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.

If you have received this email in error please notify postm...@ardescosolutions.com

asif.r...@gmail.com

unread,
Feb 28, 2017, 1:34:21 AM2/28/17
to Selenium Users, mikego...@gmail.com
Hi micksy

I an new to js and selenium webdriver. I used your code for the same purpose as defined by author chsonnu. But I received an error on executeScript in last line. Error says: Cannot find symbol: executeScript. Can  you tell me how to resolve this?

Serguei Kouzmine

unread,
Mar 1, 2017, 5:46:04 PM3/1/17
to Selenium Users, mikego...@gmail.com
You need to cast it, there are several valid syntax for that e.g.
if (driver instanceof JavascriptExecutor) {
JavascriptExecutor javascriptExecutor = JavascriptExecutor.class
.cast(driver);
return javascriptExecutor.executeScript(script, arguments);

Thanks

Serguei Kouzmine

Moises Siles

unread,
Mar 1, 2017, 9:52:49 PM3/1/17
to seleniu...@googlegroups.com, mikego...@gmail.com
Why you need to get the xpath?

Enviado desde mi iPhone
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.

To post to this group, send email to seleniu...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages