In this post, we will present a way to auto-generate robust and short XPaths for the two most common HTML elements automation interacts with – buttons and input elements. We have tested this against more than 50 commonly used websites like Facebook, LinkedIn, Citibank, IRCTC, etc.
Why this post?
The foundation for robust GUI automated checks is writing good element locators. Xpath is one locator strategy used for selecting nodes from Document Object Models (DOM) like XML, HTML, etc. Generating XPaths manually is a routine and time-consuming task.
As part of simplifying our test writing process, we came up with a thought to write a utility script which will identify and auto-generate robust and simple XPaths. We started off with generating XPaths for Input and Button fields of a webpage using the general locators like id, name, class etc. The Python program in this post will demonstrate how to create XPath expressions automatically for input and button tags. Whenever there is a requirement to automate a webpage, the tester can simply run this script which would generate a bunch of XPaths without any human intervention.
What should be considered when writing an XPath?
Most of us rely on extracting the XPaths from browser plugins or tools. These tools have got limitations. One of the limitations is that they frequently produce absolute XPaths (meaning all the way from the HTML tag!) which are long and extremely flaky. Other smarter tools seem to have trouble producing unique XPaths in anything but the simplest conditions. Generally, we locate the elements using their unique attributes but some elements do not have unique attributes. Locating such elements is difficult because the XPath generated will have multiple matching elements. It is important to consider the following points while choosing an XPath. A good locator is:
- Unique – XPath should have only one candidate element (Unique).
- Descriptive – It is easy to identify the element easily when the XPath is descriptive.
- Shorter in length – You will have multiple XPath options. A shorter XPath shall be selected to make it more readable.
- Valid even after changes to a page – XPath should be selected in such a way it is valid even after changes in DOM.
Overview of our Python Utility script
We made an effort to write a script which will auto-generate XPaths for Input and Button elements in the webpage and also check for the uniqueness of the generated XPath. This will save the automation time and effort. In the coming sections, we will be talking about below items:
- Accept a URL and parse the page content using BeautifulSoup
- For each element, check for the existence of the attribute and guess the XPath
- Check for uniqueness of the generated XPath
- How we tested this utility
- Putting it all together
Note:- If the XPath generated is not unique or if the HTML page does not have the attribute mentioned for the given tag then our script does not generate any XPaths.
Accept a URL and Parse the page content using BeautifulSoup
First, we need to import all the libraries that we are going to use. We used python module BeautifulSoup to extract and parse the HTML content.
from selenium import webdriver from bs4 import BeautifulSoup |
Our main method starts with declaring a variable which accepts the input URL of the page.
#Get the URL and parse url = input("Enter URL: ") |
The next step is to parse the page into a BeautifulSoup format. We used selenium’s execute_script() function to get the inner HTML of a page and return it as a string to the Python script. This method takes as a parameter a string of Javascript code that it executes inside of the browser.
#Parsing a page with BeautifulSoup page = driver.execute_script("return document.body.innerHTML").encode('utf-8').decode('latin-1') #returns the inner HTML as a string soup = BeautifulSoup(page, 'html.parser') |
For each element, check for existence of the attribute and guess the XPath
Now we have a variable, soup, containing the HTML of the page. Here’s where we can start coding the part that extracts the data. BeautifulSoup can help us get into these layers and extract the content with find_all() method. Using this method we are going to fetch all the Input and Button tags from the HTML page. We are passing the ‘soup’ as an argument for generate_xpath method.
#execute generate_xpath if xpath_obj.generate_xpath(soup) is False: print ("No XPaths generated for the URL:%s"%url) |
If the webpage doesn’t have any inputs and buttons it throws a print message saying that there are no tags to generate the Xpaths for this URL.
Now let us look into the generate_xpath() logic. We first initialized few list variables for element lists and attribute lists. guessable_elements lists consists of element lists and known_attribute_list consists of attribute lists. We are looping over element lists (as of now we are doing this only for input and button elements). Next for each element we will loop over the attribute lists and check for the attribute existence and then guess the XPath. Please note that we have declared the attribute lists based on the order of importance of attribute occurrence. For eg:- If ‘id‘ is not available for that element, next we are checking for ‘name‘ attribute and so on. Also variable names method below to get variable names.
def generate_xpath(self,soup): "generate the xpath and assign the variable names" result_flag = False for guessable_element in self.guessable_elements: self.elements = soup.find_all(guessable_element) for element in self.elements: if (not element.has_attr("type")) or (element.has_attr("type") and element['type'] != "hidden"): for attr in self.known_attribute_list: if element.has_attr(attr): locator = self.guess_xpath(guessable_element,attr,element) if len(driver.find_elements(By.XPATH,locator))==1: result_flag = True variable_name = self.get_variable_names(element) # checking for the unique variable names if variable_name != '' and variable_name not in self.variable_names: self.variable_names.append(variable_name) print ("%s_%s = %s"%(guessable_element, variable_name.encode('utf-8').decode('latin-1'), locator.encode('utf-8').decode('latin-1'))) break else: print (locator.encode('utf-8').decode('latin-1') + "----> Couldn't generate appropriate variable name for this xpath") elif guessable_element == 'button' and element.getText(): button_text = element.getText() if element.getText() == button_text.strip(): locator = xpath_obj.guess_xpath_button(guessable_element,"text()",element.getText()) else: locator = xpath_obj.guess_xpath_using_contains(guessable_element,"text()",button_text.strip()) if len(driver.find_elements(By.XPATH, locator))==1: result_flag = True #Check for utf-8 characters in the button_text matches = re.search(r"[^\x00-\x7F]",button_text) if button_text.lower() not in self.button_text_lists: self.button_text_lists.append(button_text.lower()) if not matches: # Striping and replacing characters before printing the variable name print ("%s_%s = %s"%(guessable_element,button_text.strip().strip("!?.").encode('utf-8').decode('latin-1').lower().replace(" + ","_").replace(" & ","_").replace(" ","_"), locator.encode('utf-8').decode('latin-1'))) else: # printing the variable name with utf-8 characters along with language counter print ("%s_%s_%s = %s"%(guessable_element,"foreign_language",self.language_counter, locator.encode('utf-8').decode('latin-1')) + "---> Foreign language found, please change the variable name appropriately") self.language_counter +=1 else: # if the variable name is already taken print (locator.encode('utf-8').decode('latin-1') + "----> Couldn't generate appropriate variable name for this xpath") break elif not guessable_element in self.guessable_elements: print("We are not supporting this gussable element") return result_flag |
Add get variables name method
def get_variable_names(self,element): "generate the variable names for the xpath" # condition to check the length of the 'id' attribute and ignore if there are numerics in the 'id' attribute. Also ingnoring id values having "input" and "button" strings. if (element.has_attr('id') and len(element['id'])>2) and bool(re.search(r'\d', element['id'])) == False and ("input" not in element['id'].lower() and "button" not in element['id'].lower()): self.variable_name = element['id'].strip("_") # condition to check if the 'value' attribute exists and not having date and time values in it. elif element.has_attr('value') and element['value'] != '' and bool(re.search(r'([\d]{1,}([/-]|\s|[.])?)+(\D+)?([/-]|\s|[.])?[[\d]{1,}',element['value']))== False and bool(re.search(r'\d{1,2}[:]\d{1,2}\s+((am|AM|pm|PM)?)',element['value']))==False: # condition to check if the 'type' attribute exists # getting the text() value if the 'type' attribute value is in 'radio','submit','checkbox','search' # if the text() is not '', getting the getText() value else getting the 'value' attribute # for the rest of the type attributes printing the 'type'+'value' attribute values. Doing a check to see if 'value' and 'type' attributes values are matching. if (element.has_attr('type')) and (element['type'] in ('radio','submit','checkbox','search')): if element.getText() !='': self.variable_name = element['type']+ "_" + element.getText().strip().strip("_.") else: self.variable_name = element['type']+ "_" + element['value'].strip("_.") else: if element['type'].lower() == element['value'].lower(): self.variable_name = element['value'].strip("_.") else: self.variable_name = element['type']+ "_" + element['value'].strip("_.") # condition to check if the "name" attribute exists and if the length of "name" attribute is more than 2 printing variable name elif element.has_attr('name') and len(element['name'])>2: self.variable_name = element['name'].strip("_") # condition to check if the "placeholder" attribute exists and is not having any numerics in it. elif element.has_attr('placeholder') and bool(re.search(r'\d', element['placeholder'])) == False: self.variable_name = element['placeholder'] # condition to check if the "type" attribute exists and not in text','radio','button','checkbox','search' # and printing the variable name elif (element.has_attr('type')) and (element['type'] not in ('text','button','radio','checkbox','search')): self.variable_name = element['type'] # condition to check if the "title" attribute exists elif element.has_attr('title'): self.variable_name = element['title'] # condition to check if the "role" attribute exists elif element.has_attr('role') and element['role']!="button": self.variable_name = element['role'] else: self.variable_name = '' return self.variable_name.lower().replace("+/- ","").replace("| ","").replace(" / ","_"). \ replace("/","_").replace(" - ","_").replace(" ","_").replace("&","").replace("-","_"). \ replace("[","_").replace("]","").replace(",","").replace("__","_").replace(".com","").strip("_") |
Check for uniqueness of the generated Xpath
XPath should have only one candidate element which means the XPath should output one and only one element. To make sure that the given XPath returns a single element we are checking the length of the elements for each locator in the generate_xpath() method and if it is greater than 1 checking for another attribute. In this way we are making sure that the XPath is unique.
if len(driver.find_elements(By.XPATH,locator))==1: result_flag = True print (locator.encode('utf-8').decode('latin-1')) break |
In the above generate_xpath() code, we are calling three different methods guess_xpath(), guess_xpath_button() and guess_xpath_using_contains(). We shall discuss in detail about these methods in below sections.
guess_xpath()
The method guess_xpath(), accepts three arguments namely tag, attr, element, and XPath is guessed based on these arguments. To handle Unicode errors due to foreign Unicode characters we used encode() and join() functions.
def guess_xpath(self,tag,attr,element): "Guess the xpath" #Class attribute returned as a unicodeded list, so removing 'u from the list and joining if type(element[attr]) is list: element[attr] = [i.encode('utf-8').decode('latin-1') for i in element[attr]] element[attr] = ' '.join(element[attr]) self.xpath = "//%s[@%s='%s']"%(tag,attr,element[attr]) return self.xpath |
guess_xpath_button()
We are using method guess_xpath_button() to check button.getText() condition. There will be situations, where you may not able to use any HTML property rather than text present in the element. text() function helps us to find the element based on the text present in the element. Since text() is a method, it does not need ‘@’ symbol as in case of an attribute.
def guess_xpath_button(self,tag,attr,element): "Guess the xpath for buttons" self.button_xpath = "//%s[%s='%s']"%(tag,attr,element) return self.button_xpath |
guess_xpath_using_contains()
In some cases, we may have to use ‘contains‘ function which helps the user to find the element with partial values, or dynamically changing values, ‘contains‘ verifies matches with the portion of the value for text for which we don’t need the complete text but need only part of the text. In our case, while we are testing our code with few URL’s we encountered few button tags with text having leading and trailing spaces in the text. In such cases, the XPath guessed using above guess_xpath() may not work. Hence we wrote another method called guess_xpath_using_contains() which uses ‘contains‘ function and generates XPath as shown below.
def guess_xpath_using_contains(self,tag,attr,element): "Guess the xpath using contains keyword" self.button_contains_xpath = "//%s[contains(%s,'%s')]"%(tag,attr,element) return self.button_contains_xpath |
How we tested this utility
We tested this Utility with almost 50 different pages which have multiple input and button fields. We also tested with pages which don’t have any text or button fields. We came across few issues which we figured and fixed.
One of the issues we encountered is that when we ran the script in Windows, the input() command ran properly but when we ran it in Git Bash, we noticed there is a time gap in running input() command. The key to the problem is windows console prints text to the screen as soon as possible, while mingw (git bash) will wait until the application tells it to update the screen. To fix this, you can use -u flag interpreter command while running the script, which will stop python from buffering output in git bash as shown below.
python -u xpath_util.py |
Putting it all together
""" Qxf2 Services: Utility script to generate XPaths for the given URL * Take the input URL from the user * Parse the HTML content using beautifilsoup * Find all Input and Button tags * Guess the XPaths * Generate Variable names for the xpaths * To run the script in Gitbash use command 'python -u utils/xpath_util.py' """ import re from selenium import webdriver from bs4 import BeautifulSoup from selenium.webdriver.common.by import By class Xpath_Util: "Class to generate the xpaths" def __init__(self): "Initialize the required variables" self.elements = None self.guessable_elements = ['input','button'] self.known_attribute_list = ['id','name','placeholder','value','title','type','class'] self.variable_names = [] self.button_text_lists = [] self.language_counter = 1 def generate_xpath(self,soup): "generate the xpath and assign the variable names" result_flag = False for guessable_element in self.guessable_elements: self.elements = soup.find_all(guessable_element) for element in self.elements: if (not element.has_attr("type")) or (element.has_attr("type") and element['type'] != "hidden"): for attr in self.known_attribute_list: if element.has_attr(attr): locator = self.guess_xpath(guessable_element,attr,element) if len(driver.find_elements(By.XPATH,locator))==1: result_flag = True variable_name = self.get_variable_names(element) # checking for the unique variable names if variable_name != '' and variable_name not in self.variable_names: self.variable_names.append(variable_name) print ("%s_%s = %s"%(guessable_element, variable_name.encode('utf-8').decode('latin-1'), locator.encode('utf-8').decode('latin-1'))) break else: print (locator.encode('utf-8').decode('latin-1') + "----> Couldn't generate appropriate variable name for this xpath") elif guessable_element == 'button' and element.getText(): button_text = element.getText() if element.getText() == button_text.strip(): locator = xpath_obj.guess_xpath_button(guessable_element,"text()",element.getText()) else: locator = xpath_obj.guess_xpath_using_contains(guessable_element,"text()",button_text.strip()) if len(driver.find_elements(By.XPATH, locator))==1: result_flag = True #Check for utf-8 characters in the button_text matches = re.search(r"[^\x00-\x7F]",button_text) if button_text.lower() not in self.button_text_lists: self.button_text_lists.append(button_text.lower()) if not matches: # Striping and replacing characters before printing the variable name print ("%s_%s = %s"%(guessable_element,button_text.strip().strip("!?.").encode('utf-8').decode('latin-1').lower().replace(" + ","_").replace(" & ","_").replace(" ","_"), locator.encode('utf-8').decode('latin-1'))) else: # printing the variable name with utf-8 characters along with language counter print ("%s_%s_%s = %s"%(guessable_element,"foreign_language",self.language_counter, locator.encode('utf-8').decode('latin-1')) + "---> Foreign language found, please change the variable name appropriately") self.language_counter +=1 else: # if the variable name is already taken print (locator.encode('utf-8').decode('latin-1') + "----> Couldn't generate appropriate variable name for this xpath") break elif not guessable_element in self.guessable_elements: print("We are not supporting this gussable element") return result_flag def get_variable_names(self,element): "generate the variable names for the xpath" # condition to check the length of the 'id' attribute and ignore if there are numerics in the 'id' attribute. Also ingnoring id values having "input" and "button" strings. if (element.has_attr('id') and len(element['id'])>2) and bool(re.search(r'\d', element['id'])) == False and ("input" not in element['id'].lower() and "button" not in element['id'].lower()): self.variable_name = element['id'].strip("_") # condition to check if the 'value' attribute exists and not having date and time values in it. elif element.has_attr('value') and element['value'] != '' and bool(re.search(r'([\d]{1,}([/-]|\s|[.])?)+(\D+)?([/-]|\s|[.])?[[\d]{1,}',element['value']))== False and bool(re.search(r'\d{1,2}[:]\d{1,2}\s+((am|AM|pm|PM)?)',element['value']))==False: # condition to check if the 'type' attribute exists # getting the text() value if the 'type' attribute value is in 'radio','submit','checkbox','search' # if the text() is not '', getting the getText() value else getting the 'value' attribute # for the rest of the type attributes printing the 'type'+'value' attribute values. Doing a check to see if 'value' and 'type' attributes values are matching. if (element.has_attr('type')) and (element['type'] in ('radio','submit','checkbox','search')): if element.getText() !='': self.variable_name = element['type']+ "_" + element.getText().strip().strip("_.") else: self.variable_name = element['type']+ "_" + element['value'].strip("_.") else: if element['type'].lower() == element['value'].lower(): self.variable_name = element['value'].strip("_.") else: self.variable_name = element['type']+ "_" + element['value'].strip("_.") # condition to check if the "name" attribute exists and if the length of "name" attribute is more than 2 printing variable name elif element.has_attr('name') and len(element['name'])>2: self.variable_name = element['name'].strip("_") # condition to check if the "placeholder" attribute exists and is not having any numerics in it. elif element.has_attr('placeholder') and bool(re.search(r'\d', element['placeholder'])) == False: self.variable_name = element['placeholder'] # condition to check if the "type" attribute exists and not in text','radio','button','checkbox','search' # and printing the variable name elif (element.has_attr('type')) and (element['type'] not in ('text','button','radio','checkbox','search')): self.variable_name = element['type'] # condition to check if the "title" attribute exists elif element.has_attr('title'): self.variable_name = element['title'] # condition to check if the "role" attribute exists elif element.has_attr('role') and element['role']!="button": self.variable_name = element['role'] else: self.variable_name = '' return self.variable_name.lower().replace("+/- ","").replace("| ","").replace(" / ","_"). \ replace("/","_").replace(" - ","_").replace(" ","_").replace("&","").replace("-","_"). \ replace("[","_").replace("]","").replace(",","").replace("__","_").replace(".com","").strip("_") def guess_xpath(self,tag,attr,element): "Guess the xpath based on the tag,attr,element[attr]" #Class attribute returned as a unicodeded list, so removing 'u from the list and joining back if type(element[attr]) is list: element[attr] = [i.encode('utf-8').decode('latin-1') for i in element[attr]] element[attr] = ' '.join(element[attr]) self.xpath = "//%s[@%s='%s']"%(tag,attr,element[attr]) return self.xpath def guess_xpath_button(self,tag,attr,element): "Guess the xpath for button tag" self.button_xpath = "//%s[%s='%s']"%(tag,attr,element) return self.button_xpath def guess_xpath_using_contains(self,tag,attr,element): "Guess the xpath using contains function" self.button_contains_xpath = "//%s[contains(%s,'%s')]"%(tag,attr,element) return self.button_contains_xpath #-------START OF SCRIPT-------- if __name__ == "__main__": print ("Start of %s"%__file__) #Initialize the xpath object xpath_obj = Xpath_Util() #Get the URL and parse url = input("Enter URL: ") #Create a chrome session driver = webdriver.Chrome() driver.get(url) #Parsing the HTML page with BeautifulSoup page = driver.execute_script("return document.body.innerHTML").\ encode('utf-8').decode('latin-1')#returns the inner HTML as a string soup = BeautifulSoup(page, 'html.parser') #execute generate_xpath if xpath_obj.generate_xpath(soup) is False: print ("No XPaths generated for the URL:%s"%url) driver.quit() |
We hope this post helps you in building robust and simple XPaths using Python code for text and button fields without having to depend on any tools or browser plugins. We are also working on getting good human-friendly variable names for the generated XPaths. We will post about it shortly.
NOTE: If you found this post useful, definitely checkout our open-sourced Python test automation framework based on the page object pattern on GitHub.
References:
1. Understanding Python and Unicode
2. Scraping a JS-Rendered Page
3. Cmd and Git bash have a different result when run a Python code
4. How to flush output of print function?
5. Strings in Python 2 and Python 3
I am an experienced engineer who has worked with top IT firms in India, gaining valuable expertise in software development and testing. My journey in QA began at Dell, where I focused on the manufacturing domain. This experience provided me with a strong foundation in quality assurance practices and processes.
I joined Qxf2 in 2016, where I continued to refine my skills, enhancing my proficiency in Python. I also expanded my skill set to include JavaScript, gaining hands-on experience and even build frameworks from scratch using TestCafe. Throughout my journey at Qxf2, I have had the opportunity to work on diverse technologies and platforms which includes working on powerful data validation framework like Great Expectations, AI tools like Whisper AI, and developed expertise in various web scraping techniques. I recently started exploring Rust. I enjoy working with variety of tools and sharing my experiences through blogging.
My interests are vegetable gardening using organic methods, listening to music and reading books.
Those who are struggling in writing xpath will not be able to use this tool due to lack of information on how to use it? If you can address following questions then this will become one of the best guide to prepare xpath:
1. Which software needs to install?
2. How to execute this code?
3. Attach some screenshot
Hi Samip,
xpath_util is just a python script.
To run this script, you need to get set up with python 2.7, selenium along with web driver and need to install BeautifulSoup package using command
pip install bs4
To execute the script, use the python command mentioned in the blog. But before that, you need to add the URL in script.
And will add the screenshots soon.
Thanks,
Rohan Dudam
Can you please let me how we can do it in java
Hi,
Unfortunately we do not have a Java example.
Nice article! I love the concept of getting them automatically, which in turn could be used for proper suggestions inside an interface. People don’t need to know xpath and can still use/test them.
Thanks so much for publishing this script; it’s really helpful!
I wanted to point out that it is nearly compatible with Python3. If you change line 47 from:
`except Exception, e:`
to:
`except Exception as e:`
Then it runs in Python3 just fine. Also, is this code, or some equivalent, on a public repo like GitHub? And can we use it under the MIT license as the repo reference at the bottom of the article?
Thank you!
Matthew, happy to hear that you found this post useful. The Python 3 version of this code is part of our open-sourced repository (MIT license) – so feel free to use it. The code itself in the utils/ folder: https://github.com/qxf2/qxf2-page-object-model/blob/master/utils/xpath_util.py
Thanks for publishing this script. it’s really helpful!
Do we have same utility in Java?
Hi,
Unfortunately we do not have a Java Utility
Thanks for approach.
Does this logic supports angular based applications?
Hi Rahul,
Yes, it will work , but it won’t generate xpaths for angular specific locators for eg. model, binding etc. Currently this utility generates xpath for general locators which has attributes like – [‘id’,’name’,’placeholder’,’value’,’title’,’type’,’class’]
Thanks Rohan.
In this logic, we are mainly focusing on input and button fields and generating independent xpaths using other locator strategies. But, how how can we handle other filed like dropdown, radiobutton, limk etc..
Sai,
Yes, currently this utility script only supports input and button fields. To handle dropdowns, radio buttons and links you need to add these items in guessable_elements and write logic to guess the xpath.
How to pass the login credentials for the entered url and extract xpaths for other webpages
Hi Jyoti,
Try to modify the main function.if you already know the app URL, you can simply add steps in main to login and then store xpaths for each page you visit.
How can we extract xpaths for other webpages if login is page common
Hi Jyoti,
Could you please provide more details of your requirements?
doesn’t work
1. if len(Browser.get_driver().find_elements_by_xpath(locator)) == 1:
we can have 1 element with several default locator
2.
Exception when trying to generate xpath for:input
Python says:sequence item 0: expected str instance, bytes found
Hi Dmitry,
Thanks for pointing out. The code was written in python2, the issue you hit as `type` conversion did not happen correctly. Python 2 does these types of conversions implicitly, hence this was not noticed. We will fix the issue and inform you once the code is available in the framework.
You can get more details about this at below:
https://blog.niteo.co/strings-in-python-2-and-python-3/
Regards,
Rahul
this can response for google forms?
Hi Richard,
Right now this script generates Xpath’s for input and button tags only. If the google form has input and button tags in it, the script generates the XPath for those tags.
It is very good script to pick the zxpath elements