Last updated:
0 purchases
playwrightdompath 0.0.1
Playwright DOMPath for Python 🎭
This library implements the ChromeDevTools DOMPath functionality in Playwright.
This means that you can retrieve the CSS selector or the XPath of any element you select in your Playwright code as a Locator.
Typescript version here: https://github.com/alexferrari88/playwright-DOMPath
Installation 📦
Install with pip
pip install playwright-dompath
API Reference 📚
css_path
css_path: (element: Playwright.Locator, optimized: bool = True) -> str
xpath_path
xpath_path: (element: Playwright.Locator, optimized: bool = True) -> str
Usage 🔧
Just import the css_path or xpath_path from this module.
At the moment the tested version is the dompath_sync one while there is dompath_async, it has not been tested yet.
from playwright_dompath.dompath_sync import css_path, xpath_path
Then use either function by passing it the element you previously selected as a Locator:
searchBar = page.locator('input[name="q"]')
print("CSS Path:", css_path(searchBar))
print("XPath:", xpath_path(searchBar))
Full Example 🎁
from playwright.sync_api import sync_playwright
from playwright_dompath.dompath_sync import css_path, xpath_path
with sync_playwright() as p:
url = "https://google.com"
browser = p.chromium.launch()
page = browser.new_page()
page.goto(url)
searchBar = page.locator('input[name="q"]')
print("CSS Path:", css_path(searchBar))
print("XPath:", xpath_path(searchBar))
browser.close()
Which will output (class names may vary for you):
CSS Path: body > div.L3eUgb > div.o3j99.ikrT4e.om7nvf > form > div:nth-child(1) > div.A8SBwf > div.RNNXgb > div > div.a4bIc > input
XPath: /html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input
TODO ✅
Increase tests coverage (include edge cases and async)
Add better error handling
Contributing 🤝🏼
Feel free to fork this repo and create a PR. I will review them and merge if ok.
The above todos can be a very good place to start.
Acknowledgements 🤗
This library reimplements the Chrome DevTools DOMPath library with modifications to allow the use of Playwright's ElementHandle and Locator
CSS.escape polyfill
License 📝
MIT
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.