Skip to main content

Find Unique Xpath of any HTML/XML element

Project description

XPanther


Overview


Find Unique Xpath expressions of HTML/XML elements.

Mostly intended for Selenium and Appium automators.

• If you are a Web automator, check XPantherIde App, which is XPantherIDE, combined with GUI in a executable file, to ease your work.

• If you are an Appium automator, or want to find xpath of xml documents, check XPanther, which is part of xpanther package.


Table of content:


XPantherIDE App

How to download:

Check the latest release of this repo, or click here:

Download XPanther

You might have to disable anti-virus if you can't download it due to being falsely detected as a virus.

or install package (check below) and paste this into terminal:

python -m xpanther.launch

How it works:

XPanther captures the outerHTML of the element you click, compares it to every element in the web page using different methods to conclude anything unique. After finding unique attributes and relations, it forms all xpaths available. Then using Selenium, it starts checking every xpath if it works for the corresponding element and measures it's speed too. If all goes well, you should end up with a tested working xpath of the element you just clicked in moments!

It needs Administrator Rights for it to work because Selenium can't find the chrome.exe otherwise, and I didn't want to include chrome.exe binary inside the executable.

How to use:

  1. Open and run the executable file.
  2. Enter the url of the website, and click start!
  3. Start right-clicking over elements, and an alert pop-up will show their xpath
  4. When done, just close the browser.
  5. After closing, a output window will open, showing in a text file the output of the program, which are the xpath, the speed of xpath and language-specific Selenium code on how to find the elements.

Options:

  1. Language - Click on the dropdown and choose which programming language you want to be the Selenium code of finding the element
  2. Show All - Check it if you want all the xpaths' be shown, this is more tolerant and gives you more options. The default mode though is accurate, more strict and very sensitive to click placement, but might not give a xpath result due to last reason.

XPanther package

This package contains 2 modules, XPanther and XPantherIDE

How to install:

pip install xpanther

or search it by xpanther if you have editor with package installer.


XPanther

Import XPanther to your file like:

from xpanther import XPanther

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.

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.
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.

Options:

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.


XPantherIDE

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()

How to use:

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 alert before trying again. You can then just close the browser and the program terminates correctly.


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.



This repository is open to improvements and 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.4.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

xpanther-1.1.4-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: xpanther-1.1.4.tar.gz
  • Upload date:
  • Size: 15.5 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.4.tar.gz
Algorithm Hash digest
SHA256 8c792a2d0a7cbf886f66a40839e74485e3825cf4be5b4ac4813c1361ee74c699
MD5 de840382343d17a60d1d8f85cd021a95
BLAKE2b-256 7db00447ca6d9d36d290145cbdeab3183115776e6b897bf20af81b87d2803cc6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: xpanther-1.1.4-py3-none-any.whl
  • Upload date:
  • Size: 13.1 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 517c5bf6912cdd17033f3b8bae563be25ac4a1a60bdd0965703e3557b8bfdf7f
MD5 5ca15a4d5f692816711870506fdd8c88
BLAKE2b-256 3a3facdf5ea4c74677e0fe80dcdd898f0c5f73b3a5ceb9bf9705fd9ac135538e

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