BreadcrumbHomeResourcesBlog What Is XPath In Selenium? The Definitive Guide May 28, 2020 What is XPath in Selenium? The Definitive GuideAutomationContinuous TestingBy Shadab SiddiquiSelenium is a leading test automation framework that enables teams to build comprehensive web tests that navigate through a wide range of web elements. Table of ContentsWhat Is XPath in Selenium?What Is XPath?Types of XPath LocatorsXPath ExamplesTry XPath in Selenium With PerfectoTable of Contents1 - What Is XPath in Selenium?2 - What Is XPath?3 - Types of XPath Locators4 - XPath Examples5 - Try XPath in Selenium With PerfectoThis blog will explain different ways to use XPath in Selenium, a popular path for locating and selecting web elements as part of your automated testing efforts. Read along or jump ahead to whichever section interests you most: Related Reading: Explore codeless automation testing with Selenium. >>Back to topWhat Is XPath in Selenium?XPath in Selenium is a technique that allows you to navigate the structure of a webpage’s HTML. It also enables you to navigate through a document's XML structure. Using XPath in Selenium helps find elements that are not found by locators such as ID, class, or name.XPath is a syntax for finding elements on web pages, and XPath in Selenium can be used on both HTML and XML documents. There are other, more straightforward Selenium locators available that search for elements using tags or CSS class names. Yet they may not be sufficient to select all DOM elements of an HTML document.By using XPath, Selenium users can search for a page element in a more dynamic way. This capability gives testers the flexibility to work with locators so they are more advantageous.XPath in Selenium is just one of many things you need to know about the platform. Explore the following resources to enhance your Selenium skillset.What to Expect in the Latest Version of SeleniumTesting With Selenium: A Selenium OverviewHow to Select Elements in Selenium With Automation ScriptsHow to Use Page Object Model in SeleniumHow to Use Selenium With PerfectoBack to topWhat Is XPath?XPath is defined as XML path. It is a syntax or language for finding any element on the web page using the XML path expression. XPath is used to find the location of any element on a webpage using HTML DOM structure.In Selenium automation, if the elements are not found by the general locators like ID, class, name, etc., then XPath is used to find an element on the web page.In this tutorial, you will learn about XPath in Selenium and different XPath expressions to find complex or dynamic elements whose attributes change dynamically on a refresh of any operations.The basic format of XPath is explained below.Syntax for XPathXPath contains the path of the element situated on the web page. The standard syntax for creating XPath is as follows:XPath=//tagname[@attribute='value']// : Select the current node.Tagname: Tagname of the particular node.@: Select attribute.Attribute: Attribute the name of the node.Value: Value of the attribute.Back to topTypes of XPath LocatorsTo find the element on web pages accurately, there are a number of different types of locators. Here are a few XPath locator examples, such as ID, name, link text, and CSS path:XPath LocatorsFind different elements on a web page.IDTo find the element by the ID of the element.ClassnameTo find the element by the class name of the element.NameTo find the element by name of the element.Link TextTo find the element by the text of the link.XPathXPath is required for finding the dynamic element and traversing between various elements of the web page.CSS pathCSS path also locates elements having no name, class, or ID.Back to topXPath ExamplesThere are two types of XPath:Absolute XPathRelative XPathLet us explore both XPath types in more detail. Absolute XPathAbsolute XPath is the direct way to find the element. But the disadvantage of the absolute XPath is that if there are any changes made in the path of the element then that XPath fails.The key characteristic of XPath is that it begins with the single forward slash(/), which means you can select the element from the root node.Below is an example of an absolute XPath expression of the element shown in the below screen.Absolute XPath: /html/body/div[1]/div/div[2]/header/div/div[2]/a/imgBack to topTry XPath in Selenium With PerfectoScale Selenium test automation in the cloud with Perfecto. Perfecto integrates seamlessly with Selenium, and every other tool in your toolchain. With Perfecto and Selenium, you can:Execute web testing 50% faster than other vendors.Enjoy the scalability of Selenium Grid from the cloud.Scale your tests across VMs, real Macs, and multiple platforms.Find and fix bugs quickly with advanced test reporting.Integrate into your entire CI/CD toolchain.And our Selenium integration is just one of many features that lets Perfecto set you up for success. Perfecto powers exceptional digital experiences by combining the power of flexible test authoring, cross-platform execution, and AI-driven analytics into one secure, cloud-based web & mobile testing platform. Experience the world’s largest global testing cloud for web and mobile apps. Try Perfecto free for 14 days.StART TRIALBack to top
Shadab Siddiqui Senior Automation Solutions Engineer, Perfecto by Perforce Shadab has over six years of experience in pre-sales (SMB and enterprise markets) and over 10 years of industry experience in automated testing. He is passionate about fostering, understanding, and achieving product value. He helps clients identify and overcome business challenges through the adoption of scalable and secure technical software solutions.