I want to retrieve the full XPATH (\\html\div\...
) of an element I have previously found via e.g. page.waitForSelector()
or page.querySelector()
.
How would I do this with Playwright? I was not able to find any built-in solutions for this.
NOTE: I am using playwright-python
, but would be open to implementing JS solutions for this - given it will work.
Template (taken from https://github.com/microsoft/playwright-python
sample code):
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
for browser_type in [p.chromium, p.firefox, p.webkit]:
browser = browser_type.launch()
page = browser.new_page()
page.goto("http://whatsmyuseragent.org/")
element = page.waitForSelector(selector="text=\"My IP Address: 192.168.0.1\"")
# HERE: retrieve full XPATH of 'element'
browser.close()
EDIT:
@hardkoded
suggested the answers to this question (Get element's xpath in javascript) as a solution, but sadly the mentioned solution does not work for my use-case as I would need to retrieve the entire XPATH of an element, and shortcuts like //*[@id="THE_ID"]
will not work for me.