Retrieve xpath and css selectors from Locator objects in Playwright
Project description
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 📝
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file playwright-dompath-0.0.1.tar.gz
.
File metadata
- Download URL: playwright-dompath-0.0.1.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 42aa141e07698c8b55e9bb4240bd474424cfd70fa8201130e18aa7f548aa4686 |
|
MD5 | 16fb3e4211a4cd5eae9ec68c4c2a5542 |
|
BLAKE2b-256 | 8b11ab499e05f426d1d92f6d3879f87ad2886a51ed181510b36e3bfc7244b6aa |
File details
Details for the file playwright_dompath-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: playwright_dompath-0.0.1-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a5b2a289e8646a486412d276b6e134cef264ae7e15cfe4f40359c69f47b1366e |
|
MD5 | 5ff8b95c1a4c6af5961e1090baa0bd1e |
|
BLAKE2b-256 | 0e567302c6119b0c4e57d068348c10af0ef74991c99c54025c152ce5a1905b5b |