Skip to main content

Playwright DOMPath for Python 🎭

MIT License Python Version PyPi Version GitHub Last Commit

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 🤗

License 📝

MIT

Metadata

Release files for playwright-dompath 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for playwright-dompath 0.0.1
File Size Uploaded
playwright-dompath-0.0.1.tar.gz 7.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for playwright-dompath 0.0.1
File Interpreter ABI Platform
playwright_dompath-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 15.0 kB

Release files / playwright-dompath-0.0.1.tar.gz

Download URL playwright-dompath-0.0.1.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
42aa141e07698c8b55e9bb4240bd474424cfd70fa8201130e18aa7f548aa4686
BLAKE2b-256 checksum
How to use checksums
8b11ab499e05f426d1d92f6d3879f87ad2886a51ed181510b36e3bfc7244b6aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.5

Release files / playwright_dompath-0.0.1-py3-none-any.whl

Download URL playwright_dompath-0.0.1-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a5b2a289e8646a486412d276b6e134cef264ae7e15cfe4f40359c69f47b1366e
BLAKE2b-256 checksum
How to use checksums
0e567302c6119b0c4e57d068348c10af0ef74991c99c54025c152ce5a1905b5b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.5

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page