Skip to main content

Find Unique Xpath of any HTML/XML element

Project description

XPanther

Find Unique Xpath expressions of HTML/XML elements.

Mostly intended for Selenium and Appium automators.

This python module's objective is to find unique xpath selector for any element in a document, using unique attributes/combination of attributes/parent or child relationship methods to find and write such expression for correctly identifying that element.

How to install:

pip install xpanther

or search it by xpanther if you have editor with package installer. If it doesn't show up (happens in pycharm sometimes), go to global site-packages (pip show xpanther<--cmd command to find directory), find and copy xpanther package, and paste it to your project's venv.

This package contains 2 modules, XPanther and XPantherIDE, let's start with the powerhouse!

Import XPanther to your file like:

from xpanther import XPanther

How to use:

Insert inside XPanther() the whole HTML/XML text, which can be in the form of:

  • string,
  • the path to any local text file you might have or
  • a page url (check below for optional-parameter page_url).

Insert inside .capture() the outerHTML/whole text of element to be found:

  • as a string
  • or alternatively an integer, which represents the index of that element in the DOM, starting from 1 as the first (check below for interesting usage of index input).
XPanther(dom).capture(element)

After writing this , run the code and the xpath of that element should be returned as value and printed in the console.

Code execution illustrations:

The class has a few optional-parameters that you can change to modify it in the way you want:

  • xml (default: False): Set this to True if document is not HTML, this is because the xml document can contain uppercase characters in attributes, unlike html, so a different parser needs to be used.

  • pre_formatted (default: False): Set this to True if you have already formatted document and don't want formatter of XPanther to change anything.

  • url_input (default: False): Set this to True if you intend to insert page url intead of text or file as the dom parameter of this class.

  • child_method (default: True): Set this to False if you don't want the Xpath to be found using its children, reason for this can depend on context.

  • show_all (default: False): Set this to True if you want all possible working xpaths found and not resort to only the one chosen by program.

  • no_digits (default: False): Set this to True if you don't want element attributes that contain numbers.

  • print_output (default: True): Set this to False if you don't want the program to print anything in console but just return the xpath as a value in any variable or function.

  • speed (default: 'normal'): other valid options are 'fast' -- (less detailed, faster execution), 'slow' -- (more detailed, slower execution), or an integer (maximum is 50, which represent the number of attributes of element program can take and proccess to find combinations of). if you have a slow computer, better set it to 'fast', cause it takes lesser number of combination and lowers memory and performance consumption, thus increasing speed of execution.

For most optimal usage, keep this values as default, you have to only change to xml=True if you are not dealing with HTML, and url_input=True if you like to fetch the HTML directly from link.

Interesting way which you can utilize putting an integer as parameter to capture(), is this function like here:

for x in range(100):
    XPanther('xpath_test.txt').capture(x+1)

Literally, create and write an unique xpath for every element in the document if you want (change the number in range() accordingly).

If you are into web automation, that's where XPantherIDE comes into play.

XPantherIDE is a simple Selenium script, that tries to mimic an IDE for finding xpaths instantly on any web page with just 1 click.

Import:

from xpanther import XPantherIDE

Use:

XPantherIDE('page_url').start()

After you run the program, the browser will open, and you can start finding xpaths by right-clicking over elements. A pop up alert will appear showing the xpath, you must accept/close the pop up before trying again. You can then just close the browser and the program terminates.

The program will test each xpath if it works, it is corresponding to clicked element, and output it in the console along with its speed.

Both programs are open to improvements or new ideas!

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

xpanther-1.1.2.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

xpanther-1.1.2-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file xpanther-1.1.2.tar.gz.

File metadata

  • Download URL: xpanther-1.1.2.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for xpanther-1.1.2.tar.gz
Algorithm Hash digest
SHA256 5bc6b592758122ae57f2eae501e9f3cb414c857b662d136e8e23ddbbb536a29b
MD5 d1aa8963ac9d84f9649430030c6f54e9
BLAKE2b-256 8bd1397fe80a6e9a69a09586b371f159c5951cad477ac45d2d15e31e36515935

See more details on using hashes here.

File details

Details for the file xpanther-1.1.2-py3-none-any.whl.

File metadata

  • Download URL: xpanther-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for xpanther-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b98f000d24633d3a38641f47fe789ed5b9ad93d41dc91a715871e9177440bcd3
MD5 96bd7742371df9cf6b2f4cc1ca3ae59b
BLAKE2b-256 f97162dfd61d26041982c6f22f6229d6e0a0dfb53be31e065062d043ec56aeb2

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