Use Cases of XPath Python In Selenium With Examples
Tags: webdev, beginners, testing, programming
Published: 2024-05-13 12:06:16

Despite having a variety of programming languages to choose from, like JavaScript, Java, Typescript, C#, Ruby, etc., Python is considered the easiest to learn due to its easy-to-learn and understandable syntax. 

It has various use cases, web apps using Flask and Django, desktop apps using Tkinter, and libraries like Scikit-learn and TensorFlow for implementing AI and ML models. Testing is also a domain where Python has a stronghold with support from various testing frameworks like Selenium, [Appium](https://www.lambdatest.com/appium?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=webpage), etc. 

During testing, locating specific elements plays pivotal to recognize DOM manipulation. This manipulation allows interaction with the web elements within the user interface. This is where Python **XPath **comes into play, offering a powerful toolset for traversing the [Document Object Model](https://www.lambdatest.com/blog/document-object-model/?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=blog) and pinpointing elements efficiently.

In this article, we will cover everything, starting from what XPath Python is, how to install and use XPath Python, extraction of text from HTML, types of XPath, Using Python with various functions and mathematical operators, and some common advanced techniques.
>  **Need to [extract text from HTML](https://www.lambdatest.com/free-online-tools/extract-text-from-html?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=free_online_tools) code? Our free online tool makes it easy and fast. Save time by extracting only the text you need from HTML code in no time.**

## What is XPath and Why is it used with Selenium?

[Automation testing](https://www.lambdatest.com/automation-testing?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=webpage) is a viable substitute for [manual testing](https://www.lambdatest.com/learning-hub/manual-testing?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=learning_hub) due to the expanding variety of browser types like Chrome, Firefox, Safari, Microsoft Edge, etc., and their multiple versions. [Selenium WebDriver](https://www.lambdatest.com/learning-hub/webdriver?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=learning_hub) is an open-source application programming interface (API) used to automate the testing of web applications. 

In Selenium, [XPath](https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=blog) uses an XML path to go through a web page’s HTML code. You can refer to the following video on our [YouTube Channel](https://www.youtube.com/@LambdaTest?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=youtube) for an in-depth tutorial on Selenium WebDriver.
{% youtube HpL6EX2kjq4 %}
XPath refers to locating components and attributes inside an HTML / XML document. Similar to how file systems utilize paths to explore directories and files, XPath uses path expressions to choose specific nodes or groups of nodes within an HTML document. XPath is recommended when other [CSS locators](https://www.lambdatest.com/learning-hub/css-selectors?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=youtube) (ID, Class, etc.) are absent from an XML/HTML page.

Choosing the right element locator is a critical aspect of effective test automation. It’s not just about functionality; it’s about efficiency and accuracy. 

To understand the preferences of the testing community, we conducted a social media poll with the question, “**What is your favorite element locator in test automation?**” The responses offer valuable insights into the most favored locator strategies.

![](https://cdn-images-1.medium.com/max/2000/0*sfS7w7Dz9vIOogkg.png)

[Source](https://www.linkedin.com/feed/update/urn:li:activity:7125005859511795714/)

A considerable percentage of participants prefer XPath as their primary element locator due to its versatility and capability, making it a prominent selection for complex web elements.

XPath can be used for various purposes apart from Automation testing. Different use cases of it will be covered in the upcoming sections. First, let’s learn how to use Python XPath with Selenium and then with other libraries like ***requests***.
>  **[Extract text from JSON](https://www.lambdatest.com/free-online-tools/extract-text-from-json?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=free_online_tools) data with our powerful and easy-to-use online free tool. Get the data you need quickly and efficiently, no technical skills required.**

## How to Install and Setup Python XPath

To use Python XPath with [Selenium](https://www.lambdatest.com/selenium?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=webpage), you are only required to install and run Selenium correctly, and to use Xpath with other libraries, you need the lxml library.

## Install Selenium

1.Install Python 3 using the installer, which can be downloaded from the official website; follow the instructions and set up Python for your system(A prerequisite).

2. Now open the command prompt or terminal and run the following command:

    python --version

**Console Output:**

![](https://cdn-images-1.medium.com/max/2000/0*5TDpyyujSSk9Ixba.png)

3.Now open your preferred IDE or editor and set up the *venv *(Virtual Environment) if you are using various versions of Python. This article uses VS Code.

4.Open a VS Code Terminal and run the following command to install Selenium. The version of Selenium used in this article is 4.10.0. Check our blog on [Selenium 4](https://www.lambdatest.com/blog/selenium-with-python/?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=blog) to know more.

    pip install selenium

![](https://cdn-images-1.medium.com/max/3200/0*rZ81-mnoiQSAFKZ_.png)
>  **Get plain text from XML documents. Simply copy and paste your XML data to extract text using our online free [Extract Text from XML](https://www.lambdatest.com/free-online-tools/extract-text-from-xml?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=free_online_tools) tool. Give it a try now!**

## Install lxml Library

A robust and effective Python library for handling XML and HTML documents is the lxml. It offers a Python interface to the C-written libxml2 and libxslt libraries. With the aid of lxml, you may traverse and modify the structures of XML and HTML documents as well as create new ones.

To install the lxml library run the following command:

    pip install lxml

![](https://cdn-images-1.medium.com/max/2696/0*r5AfB5m53DF89cj4.png)

## Verifying the Selenium Installation

By running the following script you can make sure that everything is working fine. The below code will open the LambdaTest E-commerce playground, a demo website and get its title (Your Store) printed.

    from selenium import webdriver
    
    driver = webdriver.Chrome()
    
    driver.get("https://ecommerce-playground.lambdatest.io/")
    
    # Print the title of the website 
    print(driver.title)
    driver.close()

**Console Output:**

![](https://cdn-images-1.medium.com/max/2000/0*RPmjdzGjiZoidA2F.png)

## Finding an Element Using Python XPath in Selenium

Now that we are done with the installation part, let’s find an element using XPath Python in Selenium.

To find the location of a specific element in a document, you need to obtain the XPath of that element. For that, follow the steps:

1.Inspect the [website](https://www.lambdatest.com/selenium-playground/table-pagination-demo?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=webpage) by ***right-clicking > Inspect*** or (*ctrl + Shift + C or cmd + Option + C*).

2.Select the element to see the HTML code. Now right-click on the code and Go to the copy option where you can find the XPath of that element.

![](https://cdn-images-1.medium.com/max/3200/0*jsw18CIcM9tBIz5-.png)

XPath will look something like this:

    //*[@id=”table-id”]/tbody/tr[1]

![](https://cdn-images-1.medium.com/max/2000/0*m-NCoebN3gd-ifeO.png)
>  **Strip HTML is a free online tool that strips out tags and other formatting from text. Simply paste any text into the tool and click on [Strip HTML](https://www.lambdatest.com/free-online-tools/strip-html?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=free_online_tools) to remove unnecessary formatting.**

## Using Python XPath to Locate Elements

To locate an element, the *find_element()* method is used. It returns the first matching element in the HTML document. To illustrate this, we will find the element by the XPath which we copied.

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    
    driver = webdriver.Chrome()
    
    driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")
    
    row_data = driver.find_element(By.XPATH,'//*[@id="table-id"]/tbody/tr[1]')
    print("Row 1: ", row_data.text)
    driver.close()

**Console Output:**

![](https://cdn-images-1.medium.com/max/2124/0*5gT5OarEVBCqJTyl.png)

In the XPath *//*[@id=”table-id”]/tbody/tr[1]*, **tr[1]** represents the first row of the table; However, If you change the XPath to ***//*[@id=”table-id”]/tbody/tr*** still the output remains the same because the *find_element()* method will only get the first appearance of the XPath from the HTML Document.

To get all the elements, you don’t have to mention each; rather, use the *find_elements()* method to get all the elements with the same XPath.

This is how you can *use find_elements()* in the code:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    
    driver = driver = webdriver.Chrome()
    
    driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")
    
    rows = driver.find_elements(By.XPATH,'//*[@id="table-id"]/tbody/tr')
    for row in rows:
        print(row.text)
    driver.close()

Following XPath is used in the code:

![](https://cdn-images-1.medium.com/max/2000/0*wtIQodWCayqQawq1.png)

**Console Output:**

![](https://cdn-images-1.medium.com/max/2104/0*LNXJ6JxY-EYlBiTg.png)
>  **[JSON Validator](https://www.lambdatest.com/free-online-tools/json-validator?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=free_online_tools) is a free and easy-to-use tool to validate JSON data, which makes it easier to fix the issue by providing error line and character position.**

## Retrieving attribute values

The *get_attribute()* method is used to retrieve an element’s attributes, such as the *href *attribute from an anchor tag. This function will initially attempt to return the value of the specified property. It returns the value of the corresponding attribute if the requested property doesn’t exist. *None *is returned if there isn’t an attribute with that name. You can check out the list of attributes that can be fetched.

Code on how to use *get_attribute()*:

    table = driver.find_element(By.XPATH,'//*[@id="table-id"]')
    print(table.get_attribute("class"))

Following XPath is used in the code:

![](https://cdn-images-1.medium.com/max/2000/0*vcurrf-uabo4QCqH.png)

**Console Output:**

![](https://cdn-images-1.medium.com/max/2000/0*aWgUvrb35zUKkt5z.png)
>  **[HTML Validator](https://www.lambdatest.com/free-online-tools/html-validator?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=free_online_tools) is an online tool that checks HTML syntax and quality. Improve your website’s performance and functionality with ease.**

## Extracting Text Content and Manipulating Using Python

You can extract text from the element using the text property. Apart from this, you can use various methods of Python like *split(), sort(), reverse(), replace(),* and many more.

The code below uses the *split()* method to organize the data.
This is how you can use it in the code:

    rows = driver.find_elements(By.XPATH,'//*[@id="table-id"]/tbody/tr')
    data = []
    for row in rows:
        row_data = row.text.split(" ")
        if len(row_data) > 1:
            data.append(row_data)
    
    for data_row in data:
        print(data_row)

Following XPath is used in the code:

![](https://cdn-images-1.medium.com/max/2000/0*H3BmqjTqYOyPl3Q1.png)

**Console Output:**

![](https://cdn-images-1.medium.com/max/2564/0*tInIS93Kt6vJ4umn.png)

## Handling multiple matches and filtering results

You can filter the results by adding more precise conditions using XPath. For instance, the following XPath phrase may be used to discover all div elements that belong to a particular class:

    divs = driver.find_elements(By.XPATH,'//*[@id="header"]/nav/div/div/div[2]/div/div/div[1]/div')
    
    for div in divs:
        print(div.text)

XPath using in the code:

![](https://cdn-images-1.medium.com/max/2720/0*oTs_4G85wfyq7Utq.png)

**Console Output:**

![](https://cdn-images-1.medium.com/max/2000/0*1YovUh5GawCJfKR1.png)

To verify the fetched data is correct, you can check by finding the element. To do so, follow this step:

 1. Open Developer Tools using the Inspect option.

 2. In the **Elements** panel, press *Ctrl+F* or *cmd+F*.

 3. Add the XPath in the Find Box and you will get the number of appearings.

    //*[@id="header"]/nav/div/div/div[2]/div/div/div[1]/div

Refer to the below image for more understanding.

![](https://cdn-images-1.medium.com/max/3200/0*pzJ67heXz0EDKl_N.png)
>  **Minify your JS code without changing its functionality with our easy-to-use [JS Minifier](https://www.lambdatest.com/free-online-tools/js-minify?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=free_online_tools) that reduces the size of your scripts and improve website speed.**

Additionally, you may traverse and filter elements based on how they relate to other items using XPath axes and predicates. For instance, the following XPath phrase can be used to locate every element included within the button’s particular div element:

    buttons = driver.find_elements(By.XPATH,'//*[@id="header"]/nav/div/div/div[2]/div/div/div[1]/div/button')
    for button in buttons:
    print(button.text)

XPath using in the code:

![](https://cdn-images-1.medium.com/max/2992/0*IruYfxxmR0bv-zBK.png)

**Console Output:**

![](https://cdn-images-1.medium.com/max/2000/0*XtWKVMJhO8Se5poB.png)

These examples show how to use XPath in Selenium with Python at its most basic level. You can modify the XPath expressions based on the structure and properties of the HTML or XML document you are working with.

## Types of XPath in Selenium

In XPath, there are two methods for locating an element: absolute and relative. The XPath node, which can either be the starting point of the path to the node where the element is situated or a point relative to some other point along the DOM, defines the paths.

## Absolute XPath

* Absolute XPath begins at the document’s root node and descends through the element hierarchy until it reaches the target element.

* It is less dependable if the page’s structure changes because it uses the entire path starting with the root node.

* A single forward slash (/) marks the beginning of an absolute XPath expression.

* To get the Absolute XPath ***copy full Xpath*** from the **copy **option.

Example:

    /html/body/div[1]/div/section[2]/div/div/div/table

![](https://cdn-images-1.medium.com/max/2528/0*P1cK7z3AZIpDtYQH.png)
>  **Minimize the file size of your CSS files for faster loading web pages with our free online [CSS Minify](https://www.lambdatest.com/free-online-tools/css-minify?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=free_online_tools) tool. Easy and efficient minification in just a few clicks.**

## Relative XPath

* Relative XPath employs a reference point to find the target element after starting from any node in the document.

* It is more flexible and advised because it considers the element’s position relative to its parent or sibling components.

* Expressions in XPath that are relative start with a double forward slash (//).

* To get the Absolute XPath to ***copy Xpath*** from the **copy **option.

Example:

    //*[@id=”table-id”]

![](https://cdn-images-1.medium.com/max/2000/0*TBfze5ar9jsaD5sT.png)

Relative XPath expressions are commonly used in Selenium due to their flexibility and robustness. They allow you to locate elements based on their attributes, text content, or their relationship with other elements. Additionally, you can leverage various XPath axes, functions, and predicates to create more specific and powerful expressions for element locating.

## Breakdown of XPath Expressions

XPath is made of various symbols. Each one has its significance and can be used in different ways. The following table will explain each symbol in detail:

![](https://cdn-images-1.medium.com/max/2000/1*TXRtM2DuHOwrKO2RTEUPVw.png)

XPath Example Syntax:

![](https://cdn-images-1.medium.com/max/2000/0*1PINuXk19ZVfzCO4.png)

## Using Python XPath with Functions, Mathematical and logical Operators

Using functions, mathematical operators, and logical operators with Python XPath, you may build strong and adaptable expressions to pick out particular web page items. In this section, let’s look at some examples of how to combine these features:

## Functions in Python XPath

Various functions in XPath Python can be used for Strings, Numbers, Nodes, and Boolean. In this section, we will look at various functions for each of them.

### String Functions

You can carry out a variety of operations on components using XPath functions, such as extracting attributes and counting items. The following are a few often-used functions: *text(), contains(), starts-with(), concat(),* etc. Let’s look at each function in detail.

***text():***

This function helps to extract text from an HTML/XML element. It can be used when you want the test from tags like a, p, h1 to h6, and many more. In the example below, the main h1 tag is fetched, and then the text is extracted from it.

Code Example:

    driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")
     
    table_title = driver.find_element(By.XPATH,'//*[@id="__next"]/div/section[1]/div/div/h1')
    print('Title: ',table_title.text)
>  **Make your XML code easy to read and understand with our free online [XML Prettify](https://www.lambdatest.com/free-online-tools/xml-prettify?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=free_online_tools) tool. Format your code for better readability and save time!**

**Console Output:**

![](https://cdn-images-1.medium.com/max/2000/0*KYZ16WicXXo7eZCz.png)

***contains():***

This function checks whether an element contains a specific substring or not. You can compare the substring in various ways like text, ClassNames, IDs, and many more. In the below example, find the string “Roland” in the td(cell) of an HTML document.

Code Example:

    driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")
    
    table_cell = driver.find_element(By.XPATH,'//tr/td[contains(text(), "Roland")]')
    print(table_cell.text)

**Console Output:**

![](https://cdn-images-1.medium.com/max/2000/0*MfDaBFcEpIBTUULr.png)

General Format for *contains():*

![](https://cdn-images-1.medium.com/max/2684/0*Jh_SkoQwVd7v_UZW.png)

***starts-with():***

This function checks whether an element’s text starts with a specific substring. There are various ways to compare the substring, like text, ClassNames, IDs, and many more. In the below example, find the string that starts with “R” in the td(cell) of an HTML document.

Code Example:

    driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")
    
    table_cell = driver.find_element(By.XPATH,'//tr/td[contains(text(), "Roland")]')
    print(table_cell.text)

**Console Output:**

![](https://cdn-images-1.medium.com/max/2000/0*ITSxcQAW07fYaaSp.png)

General Format for *contains():*

![](https://cdn-images-1.medium.com/max/2684/0*lRuq0kiv34p5HDur.png)

***Concatenation:***

String concatenation means adding strings together. In the example below, the title and sub_title of the HTML doc are combined.

Code Example:

    driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")
    
    main_title = driver.find_element(By.XPATH,'//*[@id="__next"]/div/section[1]/div/div/h1').text
    sub_title = driver.find_element(By.XPATH,'//*[@id="__next"]/div/section[2]/div/div/div/h2').text
    combined_text = main_title + " " + sub_title
    print(combined_text) 

**Console Output:**

![](https://cdn-images-1.medium.com/max/2000/0*r1dLapsRlm38WSOl.png)
>  **Convert your XML files to JSON format without any hassle using our reliable and efficient [XML to JSON converter](https://www.lambdatest.com/free-online-tools/xml-to-json-converter?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=free_online_tools) tool. No installation or download required.**

### Numeric functions

**count(node_expression):**

This function counts the number of elements in a specified node. There is no direct function in Python; However, it can be achieved using this technique.

Code Example:

    driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")
    
    # Select the option with "10" from the drop-down to show maximum 10 rows per page
    driver.find_element(By.CSS_SELECTOR, '#maxRows option[value="10"]').click()
    
    rows = driver.find_elements(By.XPATH, '//tr/td')
     
    print(len(rows))

**Console Output:**

![](https://cdn-images-1.medium.com/max/2000/0*pL_E9F59OSLw-pQg.png)

**number():**

XPath number function converting string to a number.

Code Example:

    driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")
    
    # Select the option with "10" from the drop-down to show maximum 10 rows per page
    driver.find_element(By.CSS_SELECTOR, '#maxRows option[value="10"]').click()
    
    rows = driver.find_elements(By.XPATH, '//tr/td[1]')
    index_list = []
    for row in rows:
        if row.text !='':
            index_list.append(int(row.text))
    
    for index in index_list:
        print(index, type(index))

**Console Output:**

![](https://cdn-images-1.medium.com/max/2000/0*kHTc3TnvnbhPuGep.png)

**sum(node_expression):**

This function returns the sum of element values in a specified node.

Code Example:

    driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")
    
    # Select the option with "10" from the drop-down to show maximum 10 rows per page
    driver.find_element(By.CSS_SELECTOR, '#maxRows option[value="10"]').click()
    
    rows = driver.find_elements(By.XPATH, '//tr/td[1]')
    index_list = []
    for row in rows:
        if row.text !='':
            index_list.append(int(row.text))
    
    for index in index_list:
        print(index, type(index))

**Console Output:**

![](https://cdn-images-1.medium.com/max/2000/0*TMPWxJ9KIyFDXqgE.png)

**floor():**

floor function returns the largest integer round value that is equal to or less than a parameter value. Another way we can say the last round value is the value return of the function. To use floor() in Python, you must import the math library. eg. FLOOR(10.45) returns 10.

Code Example:

    driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")
    
    # Select the option with "10" from the drop-down to show maximum 10 rows per page
    driver.find_element(By.CSS_SELECTOR, '#maxRows option[value="10"]').click()
    
    rows = driver.find_elements(By.XPATH, '//tr/td[1]')
    index_list = []
    for row in rows:
        if row.text !='':
            index_list.append(int(row.text))
    
    for index in index_list:
        print(index, type(index))

**Console Output:**

![](https://cdn-images-1.medium.com/max/2000/0*HtMO84ODaKzBlXRA.png)

**ceiling():**

XPath ceiling function returns the smallest integer round value that is greater than or equal to a parameter value. Another way we can say the next round value is that value returns of the function. To use *ceil()* in Python, you must import the *math* library. eg. CEILING(10.45) return 11.

Code Example:

    driver.get("https://ecommerce-playground.lambdatest.io/index.php?route=product/product&product_id=42")
    
    price = driver.find_element(By.XPATH,'//*[@id="entry_216831"]/div/div/h3').text
    
    price = float(price.replace("$",''))
    discount = price - (20/price)*100
    
    print("Discounted Price: ", math.ceil(discount))
>  **[JSON to XML Converter](https://www.lambdatest.com/free-online-tools/json-to-xml-converter?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=free_online_tools) is a simple and fast tool that converts JSON to XML data. Use this tool to convert your JSON documents into valid XML for easy data exchange.**

**Console Output:**

![](https://cdn-images-1.medium.com/max/2000/0*FgTeFvQWuwReIQj1.png)

**round():**

The XPath round function returns the round number of the specified nth number of decimal places. eg. ROUND(10.45) return 10.

Code Example:

    driver.get("https://ecommerce-playground.lambdatest.io/index.php?route=product/product&product_id=42")
    
    price = driver.find_element(By.XPATH,'//*[@id="entry_216831"]/div/div/h3').text
    
    price = float(price.replace("$",''))
    discount = price - (5/price)*100
    
    print("Discounted Price: ", round(discount,3))

Console Output:

![](https://cdn-images-1.medium.com/max/2000/0*xkgPoZMbxmD1EfYd.png)

### Node Functions

**node():**

In XPath, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and root nodes. The function returns the *node* value.

Code Example:

    driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")
    
    node = driver.find_element(By.XPATH, "//tr/td[node()]").text
    print(node)

**Console Output:**

![](https://cdn-images-1.medium.com/max/2000/0*4ZXR8jRH4diWJ5Qb.png)

**last():**

This function returns the size of the total context in a given context. The name of this function lasts, so it means the function does not return the last node value.

Code Example:

    driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")
    
    last = driver.find_element(By.XPATH,'//tr/td[last()]').text
    print(last)
>  **Our [Adler32 Hash Calculator](https://www.lambdatest.com/free-online-tools/adler32-hash-calculator?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=free_online_tools) is the perfect tool to create secure hashes to protect data from unauthorized access. Start creating your hashes now!**

**Console Output:**

![](https://cdn-images-1.medium.com/max/2000/0*_p49S5D50rtLqc3h.png)

**position():**

function returns the position of an element in the set (list) of elements in a given context.

Code Example:

    driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")
    
    position = driver.find_element(By.XPATH, "//tr/td[position()]").text
    print(position)

**Console Output:**

![](https://cdn-images-1.medium.com/max/2000/0*GIGveHZsKSNQTnT7.png)

**id(dtd_id):**

function return nodes based on passed DTD unique ID.

Code Example:

    driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")
    
    id = driver.find_element(By.XPATH, '//*[@id="table-id"]').text
    print(id)

**Console Output:**

![](https://cdn-images-1.medium.com/max/2108/0*13GbyYBO96Lw92pe.png)

**name(node_expression):**

function returns the string name of the last expression (node-set).

Code Example:

    driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")
    
    id = driver.find_element(By.XPATH, '//*[@id="table-id"]').text
    print(id)

**Console Output:**

![](https://cdn-images-1.medium.com/max/2000/0*zpuXhxBgHdGFUNyB.png)

**comment():**

This function matches the comment node and returns that specific comment node.

**local-name(node_expression):**

function returns the last expression’s local string name.

![](https://cdn-images-1.medium.com/max/2028/0*4vWncj6bGW022Dix.png)
>  **Need to create secure Gost hashes? Protect your sensitive information with our [Gost hash calculator](https://www.lambdatest.com/free-online-tools/gost-hash-calculator?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=free_online_tools). Get started today and keep your data safe from hackers.**

### Boolean functions

XPath Boolean functions are used to convert an argument(as a number, string) to a boolean and return either True or False.

**boolean(number|string|node-expression|object):** returns true if expression is positive.
**true():** returns true if the passed string is a normal string.
**false(): **returns false if the passed string is not a normal string.
**lang(): **returns true if the context node language is the same as the specified string argument.

## Mathematical and logical operators

You may execute arithmetic operations and combine several criteria to generate more complicated expressions using XPath’s mathematical and logical operators. Based on a variety of parameters, these operators can be used to navigate and choose particular items.

### Mathematical Operators

There are various types of mathematical operators like **‘=’, ‘!=’, ‘+’, ‘-‘, ‘*’, ‘/’, ‘>=’, and ‘<=’**, which are provided by Python and can be used with XPath.

There are various use cases for using these operators, such as:

* **Calculating Engagement on a Social Media Post:** You are creating a feature to show the engagement on a particular post; in this case, you have to get the data and then apply some calculations to get the engagement.

* [**Dynamic Web scraping](https://www.lambdatest.com/blog/scraping-dynamic-web-pages/?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=blog):** Consider a situation where you want to scrap article data from a blogging site, but the blogs are uploaded daily. In this case, you can scrape the number of web pages available and then run the loop according to it.

Let’s look at some basic operators and how to use them with Python XPath. For the below examples, we are using a demo web application maintained by LambdaTest.

**Addition (+) and Subtraction (-)**

We are getting the total viewers and reward points to get the product’s engagement.

Code Example:

    driver.get("https://ecommerce-playground.lambdatest.io/index.php?route=product/product&product_id=42")
    
    viewers = driver.find_element(By.XPATH,'//*[@id="entry_216826"]/ul/li[2]/span[2]').text
    reward_points = driver.find_element(By.XPATH,'//*[@id="entry_216826"]/ul/li[3]/span[2]').text
    
    total_points = int(viewers) + int(reward_points)
    
    print("Total rewards: ", total_points)

**Console Output:**

![](https://cdn-images-1.medium.com/max/2000/0*EjmVmHSyV4CUq_2v.png)

**Multiplication (*) and Division (/)**

We are getting a discount(10%) on the product by the formula.

Code Example:

    driver.get("https://ecommerce-playground.lambdatest.io/index.php?route=product/product&product_id=42")
    
    price = driver.find_element(By.XPATH,'//*[@id="entry_216831"]/div/div/h3').text
    price = float(price.replace("$",''))
    discount = price - (10/price)*100
    
    
    print("Discounted Price: ", round(discount,2))

**Console Output:**

![](https://cdn-images-1.medium.com/max/2000/0*gI9Xo4m0IFUBJnri.png)

### Logical Operators

There are three main logical operators we will cover in this section.

**And operator:**

Combines two conditions, both of which must be true for the element to be selected. In the below example, there are two conditional checks: *First Name = ‘Chelsea’* **and** *email = ‘c.harper@randatmail.com’*

Code Example:

    driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")
    
    # Select the option with "10" from the drop-down to show maximum 10 rows per page
    driver.find_element(By.CSS_SELECTOR, '#maxRows option[value="10"]').click()
    
    
    rows = driver.find_elements(By.XPATH, '//tr[td[2][contains(text(), "Chelsea")] and td[4][contains(text(), "c.harper@randatmail.com")]]')
    
    # Print the text of each row
    for row in rows:
        print(row.text)

**Console Output:**

![](https://cdn-images-1.medium.com/max/2000/0*McBOOXbuMb6jiw9-.png)

**Or operator:**

Combines two conditions, either of which can be true for the element to be selected. In the below example, there are two conditional checks: *First Name = ‘Chelsea’ or email = ‘b.grant@randatmail.com’*

Code Example:

    driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")
    
    # Select the option with "10" from the drop-down to show maximum 10 rows per page
    driver.find_element(By.CSS_SELECTOR, '#maxRows option[value="10"]').click()
    
    rows = driver.find_elements(By.XPATH, '//tr[td[2][contains(text(), "Chelsea")] or td[4][contains(text(), "b.grant@randatmail.com")]]')
    
    # Print the text of each row
    for row in rows:
        print(row.text)

**Console Output:**

![](https://cdn-images-1.medium.com/max/2000/0*T7yW1HBgcksQakYd.png)

**Not operator:**

Negates a condition, selecting elements that do not meet the specified condition. In the below example, there are two conditional checks: *First Name != ‘Chelsea’ and email != ‘b.grant@randatmail.com’*

Code Example:

    driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")
    
    # Select the option with "10" from the drop-down to show maximum 10 rows per page
    driver.find_element(By.CSS_SELECTOR, '#maxRows option[value="10"]').click()
    
    rows = driver.find_elements(By.XPATH, '//tr[not(td[2][contains(text(), "Chelsea")]) and not(td[4][contains(text(), "b.grant@randatmail.com")])]')
    
    # Print the text of each row
    for row in rows:
        print(row.text)

**Console Output:**

![](https://cdn-images-1.medium.com/max/2096/0*gOaA-uuwz4jKcGge.png)

## Custom functions and extensions

XPath also allows you to define custom functions and extensions in some environments. However, the support for custom functions and extensions may vary depending on the XPath implementation and the XML/HTML processor you are using, like Selenium.

In Python, you can create a custom function that does a certain task or calculation and outputs a result. Create a function, for instance, to determine whether a given string is included in an element’s text.

Code Example:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    def contains_text(driver, element, text):
        try:
            return WebDriverWait(driver, 10).until(
                EC.text_to_be_present_in_element((By.XPATH, element), text)
            )
        except:
            return False

## Advanced XPath Techniques

There are various techniques to make XPath more reliable and reusable like Handling dynamic content, XPath- Multiple Attribute, and using variables and conditions which we are going to discuss in this section in detail.

## Handling dynamic content

The content you’re trying to locate may be dynamic, such as elements with changing IDs or classes. This is a common practice that some websites follow to prevent automating and [web scraping](https://www.lambdatest.com/blog/web-scraping-with-javascript-and-selenium/?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=blog). However, the structure stays the same in terms of hierarchy. By using XPath, you can handle this situation. To manage dynamic material, you can employ a variety of techniques:

**XPath functions**

To locate elements with partial attribute values, XPath offers operations like *contains(), starts-with(), and ends-with().*

Code Example:

    # Find a button with a dynamic ID or className
    dynamic_id = "xyz-431353"
    button = driver.find_element(By.XPATH, f"//button[contains(@id, '{dynamic_id}')]")

**Using Axes**

Use XPath axes such as following-sibling, preceding-sibling, etc., to move to the target element in relation to other already known elements.

Code Example:

    label_text = "name"
    span_element = driver.find_element(By.XPATH, f"//label[text()='{label_text}']/following-sibling::span")

## XPath- Multiple Attribute

By placing the | operator between square brackets [], you may use XPath to pick elements based on some attributes. This enables you to filter elements that fit various criteria simultaneously. To do this, you reference an attribute in the XPath expression by using the @ symbol followed by the attribute name.

![](https://cdn-images-1.medium.com/max/3200/0*RZrHU1AvIuCJs5Jo.png)
>  **Make your data tamper-proof with our [Whirlpool Hash Calculator](https://www.lambdatest.com/free-online-tools/whirlpool-hash-calculator?utm_source=devto&utm_medium=organic&utm_campaign=may_13&utm_term=bh&utm_content=free_online_tools). Create secure hashes quickly and easily with just a few clicks. Try it out now!**

## Using XPath variables and conditions

XPath supports variables and can be utilized to shorten and reuse your expressions. When utilizing your Python XPath expressions in Selenium, you can pass variables to them.

Code Example:

    from selenium.webdriver.common.by import By
    
    # Define the XPath expression with variables
    element_type = "button"
    element_text = "Submit"
    xpath_expression = f"//{element_type}[text()='{element_text}']"
    
    # Find the element using the XPath expression
    element = driver.find_element(By.XPATH, xpath_expression)

You can also use conditions in your XPath expressions using the if statement.

Code Example:

    # Find elements based on a condition (e.g., containing a specific class)
    is_enabled = True
    condition = "enabled" if is_enabled else "disabled"
    xpath_expression = f"//button[contains(@class, '{condition}')]"
    elements = driver.find_elements(By.XPATH, xpath_expression)

Keep in mind that using complicated XPath expressions can make it more difficult to read and maintain your code. When feasible, utilize CSS selectors because they are typically quicker and easier to understand. Use XPath only in circumstances where CSS selectors are insufficient. Additionally, to make locating items in your HTML more dependable and effective, choose to use distinct IDs, classes, or data properties.

## Conclusion

In a nutshell, when used with Selenium, XPath is a strong and crucial tool for web scraping and automating online applications. Using an element’s attributes, textual content, or connections to other elements enables developers and testers to find that element in an HTML or XML document. Python XPath offers several methods, tools, and axes for effectively navigating and choosing elements.

Before using XPath with Selenium, you must install and configure the necessary Python libraries. When everything is set up, you can find DOM items using XPath expressions. **Relative XPath** allows you to navigate between elements based on their relationships, while **Absolute XPath** specifies the whole route from the root. XPath is adaptive to changing web page structures because it can handle dynamic material utilizing functions like *contains(), starts-with(), *and more.

Using XPath, you may extract attribute values and text content to validate and modify data on web pages. You can manage numerous matches and filter outcomes based on particular criteria using XPath. Advanced XPath techniques, such as using variables and conditions and creating custom functions to make XPath statements more dynamic and reusable.

To sum up, XPath is an excellent option for finding objects on web pages, navigating intricate DOM structures, and effectively handling dynamic material thanks to its variety and potent capabilities. Learning XPath enables developers and testers to create automated scripts that are reliable and maintainable, thereby enhancing the quality and efficiency of web applications.

## Frequently Asked Questions (FAQs)

### Does XPath work with HTML in Python?

Yes, Python supports using XPath with HTML. A strong query language used to browse and pick elements from XML or HTML documents is called XPath. It enables you to extract particular information or elements from the text depending on their positioning and characteristics. Using XPath will be fruitful when unique identifiers are not present like ID, or Class.

### How to check if the XPath element exists in Selenium Python?

The *find_elements()/ find_element()* method in Selenium Python can be used to determine whether an element is present based on an XPath expression. A list of web elements that match the provided XPath is returned by the *following* method. The element does not exist on the page if the list is empty, which means no elements were detected. By using a simple if else statement or try/catch method you can check for the element.

### How to find XPath in Selenium Python?

In Selenium Python, determining an element’s XPath is a rather simple process. To find an element’s XPath, you can do so in a number of different ways. Here are three such methods: 1. Using browser developer tools, 2. Using Selenium’s *find_elements()/ find_element()*, and 3. Using browser extensions.