Javatpoint Logo
Javatpoint Logo

What is XPath in Selenium with Python?

Selenium in Python

Selenium is an open-source Python framework that offers API for writing functional tests using Selenium. It is used for cross-browser testing of web applications. Various scripting languages support this library, like Python, Java, Node.js, C#, and Ruby.

Selenium is developed to drive the browser and execute tests for different applications used by developers, project managers, and quality assurance engineers to speed up the time. Over many years, Selenium evolved from a technology created to drive a single browser and run tests to a standard protocol for software to interact with a browser as if it were a human, an in-browser tool for recording and replaying tests (Selenium IDE), a distributed computing grid for running multiple tests at once (Selenium Grid), and many more.

Using Web drivers, Selenium navigates the web browsers and does the required testing like searching web pages, scrolling them, parsing them, running multiple tests simultaneously, etc.

Installing Selenium Library in Python

The Selenium library in Python can be installed using a command prompt or any Python terminal using this command:

The first step to implementing a framework in Selenium is to find the web elements in the browser. The XPath helps to find these web elements and navigate the web browser.

XPath in Selenium

The XPath in the Selenium library is the path to locate the web elements dynamically. XPath is referred to as XML path language. The XPath works with the Selenium WebDriver, for navigating the web pages using its HTML structures by getting the required tags. It can locate the elements of both HTML and XML documents. XPath can navigate web pages accurately for automation testing.

When the Web Driver navigates any web browser, it creates a DOM (Document Object Model) structure. Then, the XPath expression finds the web elements within the Document Object Model Structure.

Syntax of XPath in Selenium

The XPath in the selenium library can be represented as:

Here,

  • driver is the web driver
  • find_element is the method to locate the web elements
  • XPath is the method to get the XPath
  • XPath is the path that locates the web element in the browser through the HTML structure.

Format of XPath in Selenium

Here,

  • tag_name is the tag name of the current node
  • @ is the Select Attribute
  • Attr is the attribute of the node
  • Val is the value of the attribute

Using XPath in Selenium

Let's understand how to get web elements using the XPath

Example 1: A program to navigate the web page using Selenium and get the web element using XPath.

Code:

Importing the required packages

2. Opening the web browser using web driver

3. Getting the XPath for getting the web element

Output:

<selenium.webdriver.remote.webelement.WebElement (session="b676ae0461ecd5fe1870a8c44dcb9200", element="9A35AA5EC57622AD748FCBB7F1FE36AD_element_4230")>

Browser Output:

What is XPath in Selenium with Python

Explanation:

In this code, using the selenium web drivers, we have opened the web page "javatpoint" and, using find_element with the XPath, we have traced the search bar option.

The web driver will open a new browser in a new window and then navigate according to the requirements.

How to Find Elements by XPath in Selenium?

The position of the specified web element must first be determined by its XML path to change it in the DOM structure. XPath is often the most straightforward approach to begin using locators in Selenium for this task. The preferred locator for viewing components on a page is XPath. XPath can extract data from web elements in an XML or HTML document if general locators, like ID, name, class name, etc., cannot identify the element.

Various locators in Selenium are used to search elements using their tags or CSS class names. Locating the elements with the ID and CSS tags is the fastest, but we can move XPath to CSS tags. More complex and deep searches can be done with the XPath Locator.

There are different methods by which XPath can be created:

  • Parent
  • Child
  • Ancestor
  • Self
  • Starts_with()
  • Ends_with()
  • Preceding

There are many methods to create XPath besides this.

Different Types of XPath in Selenium

Two types of XPath are used to locate an element are:

  1. Absolute XPath
  2. Relative XPath

The pathways are specified by their XPath nodes, which can either be the path's starting point to the node where the element is situated or a point along the DOM.

Absolute XPath

The Absolute XPath is a type of XPath that is used to locate the elements in their absolute terms. It has the location of all elements from the starting node (starting of the path) to the destination element. It can cause problems navigating with the Absolute XPath if there is any change in the node or adjustment of the tag of which the XPath is defined.

Example of the Absolute XPath

The Absolute XPath always starts with a forward slash "/".

Relative XPath

The Relative XPath is a type of XPath used to locate the elements from any part of the web page. The XPath can also be relative when the element has an ID or name attribute. It can start from the middle of the HTML Structure. It is much shorter than the Absolute Path.

Example of the Relative XPath

The Relative XPath always with a double forward slash "//".







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA