Skip to main content

intuitive xpath generator

Project description

yet another xpath generator

Create an xpath with an chainable and intuitive API.

Quickstart

Create the most simple xpath for a div element:

from yaxp import xpath

# //div
xpath.div

xpath objects are chainable:

# //div//h1
xpath.div.h1

Any keyword argument you pass will add a filter for an attribute with the name and value of the keyword:

# //div[@role="cell"]
xpath.div(role="cell")

Alternatively, use the by() method to specify attributes:

# //*[@class="main"]
xpath.by(_class="main")

An _ at the beginning of the attribute name will be removed, this is helpful for attributes that represents python keywords like "class" or "id":

# //div[@class="cell"][@id="myid"]
xpath.div(_class="cell", _id="myid")

If the value of an attribute starts with an asteric (*), the xpath matches any element that has the following text as a substring in this attribute

# //div[contains(@class, 'mycl')]
xpath.div(_class="*mycl")               

Alternatively, you can use the contains() function to filter for subtrings. the following statement is equal to the above:

# //div[contains(@class, 'mycl')]
xpath.div.contains(_class="mycl")

xpath supports "nested predicates", i.e. you can filter for specific sub-elements, while the xpath itself will point to the parent element (div in this example):

# //div[./span[@class='mycl']]
xpath.div.has(xpath.span(_class="mycl"))

Nested predicates are chainable:

# //div[./span[@class='mycl'][./p[text()="hello"]]]
xpath.div.has(xpath.span(_class="mycl")).has(xpath.p(text="hello"))

As you can see in the example above, a text attribute will be converted to "text()". In order to avoid this, use "_":

# //p[@text="hello"]]]
xpath.p(_text="hello"))

An "_" attrbute will be converted to ".":

# //p[contains(., "world")]
xpath.p(_="*world")

If the value of an attribute starts with a hashtag (#), the xpath matches any element that has the following text as a full word in this attribute:

# //div[contains(concat(' ', normalize-space(@class), ' '), ' myclass ')]
xpath.div(_class="#myclass")

Any combination of the features are allowed:

# //span[contains(@class, "mycl")][@placeholder="huhu"]
xpath.span(_class="*mycl", _placeholder='huhu')

An _ in the role will be converted to a ".":

# //Android.Container[@id="huhu"]
xp.Android_Container(_id="huhu")

Use double __ if you need an _:

# //Android_Container[@id="huhu"]
xp.Android__Container(_id="huhu")

Further reading

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yaxp-1.0.1.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

yaxp-1.0.1-py2.py3-none-any.whl (4.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file yaxp-1.0.1.tar.gz.

File metadata

  • Download URL: yaxp-1.0.1.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for yaxp-1.0.1.tar.gz
Algorithm Hash digest
SHA256 ba853b5dc949b10abff39937df78e78336e3d88ad8b2e3b240c30dbaf15f1559
MD5 77432f5797f0999df06ce97489cfee20
BLAKE2b-256 1154b89fa17863a86ea7fda19817ebca3344c11e9405d511b2f93d9ffabd24d8

See more details on using hashes here.

File details

Details for the file yaxp-1.0.1-py2.py3-none-any.whl.

File metadata

  • Download URL: yaxp-1.0.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for yaxp-1.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 26d7ca70b57dd6aadc0f3a682902330ae6ffcf5c359365fe997b3c67287d9e72
MD5 c76f27fd7a6ef439b228aeac490f3063
BLAKE2b-256 91e0bc515d0ddeb269cd859aae4a9b0aceb63d6df58da4ad907f499ec19db842

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page