BreadcrumbHomeResourcesBlog Object Identification In Selenium Using XPath March 20, 2017 Object Identification in Selenium Using XPathAutomationBy Uzi EilonSelenium is a popular open-source framework used for automating web browsers. It provides a range of features to interact with web elements, such as buttons, text fields, and dropdowns, enabling testers and dev teams to automate web application testing. One crucial aspect of Selenium automation is object identification, which involves locating and interacting with web elements on a webpage. XPath is commonly used for object identification in Selenium.In this blog, we will explore the concept of object identification in Selenium using XPath, and how it can enhance your Selenium automation tests and increase the overall quality of your web applications. Back to topWhat Is Object Identification in Selenium Using XPath?Object identification in Selenium using XPath refers to the process of locating web elements on a page by leveraging XPath, enabling flexible and reliable element identification. Back to topWhy Object Identification in Selenium Is ImportantObject identification in Selenium is important. It helps you handle a dynamic list of object id changes, based on the number of rows. It also provides better validation options for the list of objects in one screen. It helps you find elements where the relative XPath is different across devices. And, finally, it helps you get dynamic data from the screen. Now that you know why object identification is important, let's cover how to do it. Table of ContentsWhat Is Object Identification in Selenium Using XPath? Why Object Identification in Selenium Is ImportantHow-To: Object Identification in SeleniumGet More Out of SeleniumTable of Contents1 - What Is Object Identification in Selenium Using XPath?2 - 3 - Why Object Identification in Selenium Is Important4 - How-To: Object Identification in Selenium5 - Get More Out of SeleniumBack to topHow-To: Object Identification in SeleniumHere's how to identify objects in Selenium. Your first step is to get an object list. In Selenium, you can get list of objects with specific property. Then you can manipulate it in the code.Let's use the following as our working example:I would like to get the cars category list and number of car in each category.1. Find the Common PropertyIn this example, all the items has the same class property : ui-link-inherit. 2. Use findElements CommandIn the code, use findElements command with XPath “contains” option with the specific property.List<WebElement> objList = webDriver.findElements(By.xpath("(//div[fusion_builder_container hundred_percent="yes" overflow="visible"][fusion_builder_row][fusion_builder_column type="1_1" background_position="left top" background_color="" border_size="" border_color="" border_style="solid" spacing="yes" background_image="" background_repeat="no-repeat" padding="" margin_top="0px" margin_bottom="0px" class="" id="" animation_type="" animation_speed="0.3" animation_direction="left" hide_on_mobile="no" center_content="no" min_height="none"][@id=\"Content-\"])/ul/li/following::*[contains(@class,\"ui-link-inherit\")]"));3. Loop Over the WebElement ListLoop over the WebElement list to work with the objects.4. Set an Anchor to the ListIn this example I need to set an anchor to the list as part of my XPath because the page contains other object with the same class property which I want to exclude.Part 1: The first part is setting the anchor for the elements I looking for://div[@id=\"Content-\"])/ul/li/following::Part 2: the second part is getting all the objects with the calss ui-link-inherit*[contains(@class,\"ui-link-inherit\")There's a code example in GitHub.Throughout this blog, I've used XPath as an example. There are options for Selenium locators, too, besides XPath. Here's a quick list of locators that can help you identify elements.:IDClassNameNameLink TextPartial Link TextTagNameCSS SelectorBack to topGet More Out of SeleniumSelenium is a great open source framework for automated web testing. But it can't do it all.Perfecto pairs with Selenium to to make it even better. For example:Perfecto makes test authoring easier with codeless and BDD options.Perfecto can scale large amounts of test suites.Perfecto accelerates your processes with bursting and parallel testing.See for yourself why teams everywhere pair Selenium with Perfecto. Try it for free today.START Trial Related ContentLimitations of Selenium & How Perfecto Can HelpConsiderations for Selenium Grid in the CloudCypress vs. Selenium: What’s the Right Cross-Browser Testing Solution for You?Back to top
Uzi Eilon Director of Technology, Perfecto Uzi joined Perfecto Mobile seven years ago after a fifteen year career as a software developer and manager at IDF, Netrialty, Comverse and Sundisk. Over the past seven years, Uzi has helped grow Perfecto by managing expanding R&D teams and leading sales engineering teams. His fields of expertise include agile methodologies, mobile application testing, automation tools, and defining customer projects and on-boarding.