SlideShare ist ein Scribd-Unternehmen logo
Using XPath in
Selenium: All you
need to know
Copyright © 2023 HeadSpin, Inc. All Rights Reserved.
Introduction
Navigating the complex web world as an automation tester aims to ensure the delivery of an
application that is bug-free and seamless and offers exceptional user experiences. One common
challenge on this journey is locating web elements accurately and efficiently to interact with them.
Selenium WebDriver helps identify web elements efficiently with various tools and locators. Today,
several enterprises are leveraging XPath owing to its greater flexibility and compatibility with old
browsers, making it one of its most powerful and versatile locators.
Copyright © 2023 HeadSpin, Inc. All Rights Reserved.
What is XPath in Selenium?
XPath, or XML Path Language, is a language for navigating an XML document and
selecting nodes. In the context of Selenium WebDriver, XPath is used as a locator to
find web elements on a page. It is a powerful tool that can navigate the webpage's
HTML structure, making it extremely useful when other simple locators like ID or
class fail to find the elements reliably.
Copyright © 2023 HeadSpin, Inc. All Rights Reserved.
There are primarily two types of
XPath used in Selenium:
• Absolute XPath
• Relative XPath
Types of XPath in
Selenium
Copyright © 2023 HeadSpin, Inc. All Rights Reserved.
It's a direct path from the root element to the desired element. It starts from the root
node and ends with the desired node, providing a complete path. However, it's brittle
and can break with small changes in the web page's structure.
Here's a Selenium XPath example for how you might use an absolute XPath in your
Selenium code. Suppose you have the following HTML structure:
<html>
<body>
<div>
<p>Hello, world!</p>
</div>
</body>
</html>
Absolute XPath(1/2)
Copyright © 2023 HeadSpin, Inc. All Rights Reserved.
Copyright © 2023 HeadSpin, Inc. All Rights Reserved.
To find the <p> tag using an absolute XPath, you would start at the root <html> tag and
provide the full path to the <p> tag:
In this case, the XPath "/html/body/div/p" represents the absolute path from the root
<html> tag to the desired <p> tag.
In several scenarios, however, absolute XPath is not recommended unless necessary
because it's brittle, and any change in the HTML structure may cause your test to fail.
WebElement paragraph = driver.findElement(By.xpath("/html/body/div/p"));
Absolute XPath(2/2)
Relative XPath(1/2)
It starts from any node and ends with the node you
want to select. It's more flexible and preferred in
most cases, as it's not affected by changes in other
parts of the HTML structure. A relative XPath allows
you to locate elements starting from any location
within the HTML document, not just the root. The
relative XPath expression usually starts with //.
Here's an example of using relative XPath in
Selenium with Python:
```python
from selenium import webdriver
# Create a new instance of the Firefox driver
driver = webdriver.Firefox()
# Navigate to a website
driver.get("https://example.com")
# Find an element using relative XPath
element = driver.find_element_by_xpath("//div[@id='myDiv']/p[2]")
# Perform actions on the element
element.click()
# Close the browser
driver.quit(
Copyright © 2023 HeadSpin, Inc. All Rights Reserved.
Copyright © 2023 HeadSpin, Inc. All Rights Reserved.
In the example mentioned in the previous slide , we first import the necessary modules from the Selenium library. Then,
we create a new instance of the Firefox driver and navigate to a website (in this case, "https://example.com").
Next, we find an element using a relative XPath expression. The XPath used in this example selects the second `<p>`
element inside a `<div>` element with the id "myDiv". You can modify the XPath expression to suit your specific needs.
After finding the element, you can perform various actions on it, such as clicking it, entering text, or retrieving its
attributes.
Finally, we close the browser using the `quit()` method to clean up and release the resources used by the driver.
Using relative XPaths is generally recommended over absolute XPaths because they are more resilient to page
structure changes.
Relative XPath(2/2)
Types of XPath locators
This locator allows you to identify an element by its id
attribute.
XPath locator by ID
This locator can identify elements based on their
class attribute.
XPath locator by class name
XPath locator by tag name
This locator can identify elements by their HTML
tag name.
This locator identifies elements by their name
attribute.
XPath locator by name
XPath locator by text
This locator identifies elements based on their
inner text.
XPath locator using contains
This locator can identify elements based on a
substring of one of their attribute values.
XPath locator using starts-with
This locator identifies elements whose attribute
values start with a particular string.
Path locator using ends-with
This locator can identify elements whose
attribute values end with a particular string.
Copyright © 2023 HeadSpin, Inc. All Rights Reserved.
What is chained XPath in
Selenium?
Chained XPath in Selenium is a concept where multiple XPaths are used in conjunction to
locate an element that might not be uniquely identifiable by a single XPath expression. In other
words, instead of writing one absolute XPath, we can separate it into multiple relative XPaths.
This approach can be specifically useful when dealing with complex or dynamic web structures
where elements are not easily accessible through single, unique identifiers. Chaining XPaths
can provide more precision and robustness in element location strategy, thus making the
automation scripts more stable.
Copyright © 2023 HeadSpin, Inc. All Rights Reserved.
What are XPath Axes?
Following Ancestor Child Parent
XPath Axes are used for finding dynamic elements when normal XPath element search methods like
name, ID, class name, etc., aren't possible. XPath Axes navigate through elements in the XML
structure of a webpage. They allow you to locate elements based on their relationship with other
elements, like parent, sibling, child, ancestor, or descendant. Here are few XPath methods:
Copyright © 2023 HeadSpin, Inc. All Rights Reserved.
Copyright © 2023 HeadSpin, Inc. All Rights Reserved.
How HeadSpin helps
streamline Selenium
utilization
HeadSpin offers a robust AI-driven testing Platform
with easy integration with multiple automation
frameworks to improve testing efficiency and ship
faster to the market.
1
Support for Selenium Webdriver
2
Parallel Testing
3
Integration with CI/CD pipelines
4
Integration with CI/CD pipelines
4
Integration with CI/CD pipelines
4
Integration with CI/CD pipelines
Conclusion
XPath is critical in Selenium testing, offering a robust method for locating elements within
the web page's DOM. Understanding and utilizing XPath's capabilities can greatly improve
the effectiveness and efficiency of your Selenium tests. Moreover, with platforms like
HeadSpin, you can enhance your testing capabilities further, leveraging its unique features
aligned with Selenium standards.
Reference Link: https://www.headspin.io/blog/using-xpath-in-selenium-effectively
Copyright © 2023 HeadSpin, Inc. All Rights Reserved.

Weitere ähnliche Inhalte

Ähnlich wie Using XPath in Selenium_ All you need to know.pdf

Automated_Testing_Selenium
Automated_Testing_SeleniumAutomated_Testing_Selenium
Automated_Testing_Selenium
Jagdish Kaushal
 
Semantics In Declarative Systems
Semantics In Declarative SystemsSemantics In Declarative Systems
Semantics In Declarative Systems
Optum
 
Automatedtestingwithselenium shubham jain
Automatedtestingwithselenium shubham jainAutomatedtestingwithselenium shubham jain
Automatedtestingwithselenium shubham jain
Prashant Gurav
 
QER : query entity recognition
QER : query entity recognitionQER : query entity recognition
QER : query entity recognition
Dhwaj Raj
 
Mastering Test Automation: How To Use Selenium Successfully
Mastering Test Automation: How To Use Selenium SuccessfullyMastering Test Automation: How To Use Selenium Successfully
Mastering Test Automation: How To Use Selenium Successfully
SpringPeople
 
Session on Selenium Powertools by Unmesh Gundecha
Session on Selenium Powertools by Unmesh GundechaSession on Selenium Powertools by Unmesh Gundecha
Session on Selenium Powertools by Unmesh Gundecha
Agile Testing Alliance
 
New Features of Chropath for XPath and CSS Selectors | Edureka
New Features of Chropath for XPath and CSS Selectors | EdurekaNew Features of Chropath for XPath and CSS Selectors | Edureka
New Features of Chropath for XPath and CSS Selectors | Edureka
Edureka!
 
My 70-480 HTML5 certification learning
My 70-480 HTML5 certification learningMy 70-480 HTML5 certification learning
My 70-480 HTML5 certification learning
Syed Irtaza Ali
 
Advanced full text searching techniques using Lucene
Advanced full text searching techniques using LuceneAdvanced full text searching techniques using Lucene
Advanced full text searching techniques using Lucene
Asad Abbas
 
Elasticsearch quick Intro (English)
Elasticsearch quick Intro (English)Elasticsearch quick Intro (English)
Elasticsearch quick Intro (English)
Federico Panini
 
Object identification and its management
Object identification and its managementObject identification and its management
Object identification and its management
Vinay Kumar Pulabaigari
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalyst
dwm042
 
Advanced Web Development
Advanced Web DevelopmentAdvanced Web Development
Advanced Web Development
Robert J. Stein
 
Struts2
Struts2Struts2
User and group security migration
User and group security migrationUser and group security migration
User and group security migration
Amit Sharma
 
[2D1]Elasticsearch 성능 최적화
[2D1]Elasticsearch 성능 최적화[2D1]Elasticsearch 성능 최적화
[2D1]Elasticsearch 성능 최적화
NAVER D2
 
[2 d1] elasticsearch 성능 최적화
[2 d1] elasticsearch 성능 최적화[2 d1] elasticsearch 성능 최적화
[2 d1] elasticsearch 성능 최적화
Henry Jeong
 
Struts 2
Struts 2Struts 2
Struts 2
Lalit Garg
 
FluentSelenium Presentation Code Camp09
FluentSelenium Presentation Code Camp09FluentSelenium Presentation Code Camp09
FluentSelenium Presentation Code Camp09
Pyxis Technologies
 
Advanced SEO for Web Developers
Advanced SEO for Web DevelopersAdvanced SEO for Web Developers
Advanced SEO for Web Developers
Nathan Buggia
 

Ähnlich wie Using XPath in Selenium_ All you need to know.pdf (20)

Automated_Testing_Selenium
Automated_Testing_SeleniumAutomated_Testing_Selenium
Automated_Testing_Selenium
 
Semantics In Declarative Systems
Semantics In Declarative SystemsSemantics In Declarative Systems
Semantics In Declarative Systems
 
Automatedtestingwithselenium shubham jain
Automatedtestingwithselenium shubham jainAutomatedtestingwithselenium shubham jain
Automatedtestingwithselenium shubham jain
 
QER : query entity recognition
QER : query entity recognitionQER : query entity recognition
QER : query entity recognition
 
Mastering Test Automation: How To Use Selenium Successfully
Mastering Test Automation: How To Use Selenium SuccessfullyMastering Test Automation: How To Use Selenium Successfully
Mastering Test Automation: How To Use Selenium Successfully
 
Session on Selenium Powertools by Unmesh Gundecha
Session on Selenium Powertools by Unmesh GundechaSession on Selenium Powertools by Unmesh Gundecha
Session on Selenium Powertools by Unmesh Gundecha
 
New Features of Chropath for XPath and CSS Selectors | Edureka
New Features of Chropath for XPath and CSS Selectors | EdurekaNew Features of Chropath for XPath and CSS Selectors | Edureka
New Features of Chropath for XPath and CSS Selectors | Edureka
 
My 70-480 HTML5 certification learning
My 70-480 HTML5 certification learningMy 70-480 HTML5 certification learning
My 70-480 HTML5 certification learning
 
Advanced full text searching techniques using Lucene
Advanced full text searching techniques using LuceneAdvanced full text searching techniques using Lucene
Advanced full text searching techniques using Lucene
 
Elasticsearch quick Intro (English)
Elasticsearch quick Intro (English)Elasticsearch quick Intro (English)
Elasticsearch quick Intro (English)
 
Object identification and its management
Object identification and its managementObject identification and its management
Object identification and its management
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalyst
 
Advanced Web Development
Advanced Web DevelopmentAdvanced Web Development
Advanced Web Development
 
Struts2
Struts2Struts2
Struts2
 
User and group security migration
User and group security migrationUser and group security migration
User and group security migration
 
[2D1]Elasticsearch 성능 최적화
[2D1]Elasticsearch 성능 최적화[2D1]Elasticsearch 성능 최적화
[2D1]Elasticsearch 성능 최적화
 
[2 d1] elasticsearch 성능 최적화
[2 d1] elasticsearch 성능 최적화[2 d1] elasticsearch 성능 최적화
[2 d1] elasticsearch 성능 최적화
 
Struts 2
Struts 2Struts 2
Struts 2
 
FluentSelenium Presentation Code Camp09
FluentSelenium Presentation Code Camp09FluentSelenium Presentation Code Camp09
FluentSelenium Presentation Code Camp09
 
Advanced SEO for Web Developers
Advanced SEO for Web DevelopersAdvanced SEO for Web Developers
Advanced SEO for Web Developers
 

Kürzlich hochgeladen

AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
IndexBug
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
Federico Razzoli
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Project Management Semester Long Project - Acuity
Project Management Semester Long Project - AcuityProject Management Semester Long Project - Acuity
Project Management Semester Long Project - Acuity
jpupo2018
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
OpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - AuthorizationOpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - Authorization
David Brossard
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Wask
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 

Kürzlich hochgeladen (20)

AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Project Management Semester Long Project - Acuity
Project Management Semester Long Project - AcuityProject Management Semester Long Project - Acuity
Project Management Semester Long Project - Acuity
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
OpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - AuthorizationOpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - Authorization
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 

Using XPath in Selenium_ All you need to know.pdf

  • 1. Using XPath in Selenium: All you need to know Copyright © 2023 HeadSpin, Inc. All Rights Reserved.
  • 2. Introduction Navigating the complex web world as an automation tester aims to ensure the delivery of an application that is bug-free and seamless and offers exceptional user experiences. One common challenge on this journey is locating web elements accurately and efficiently to interact with them. Selenium WebDriver helps identify web elements efficiently with various tools and locators. Today, several enterprises are leveraging XPath owing to its greater flexibility and compatibility with old browsers, making it one of its most powerful and versatile locators. Copyright © 2023 HeadSpin, Inc. All Rights Reserved.
  • 3. What is XPath in Selenium? XPath, or XML Path Language, is a language for navigating an XML document and selecting nodes. In the context of Selenium WebDriver, XPath is used as a locator to find web elements on a page. It is a powerful tool that can navigate the webpage's HTML structure, making it extremely useful when other simple locators like ID or class fail to find the elements reliably. Copyright © 2023 HeadSpin, Inc. All Rights Reserved.
  • 4. There are primarily two types of XPath used in Selenium: • Absolute XPath • Relative XPath Types of XPath in Selenium Copyright © 2023 HeadSpin, Inc. All Rights Reserved.
  • 5. It's a direct path from the root element to the desired element. It starts from the root node and ends with the desired node, providing a complete path. However, it's brittle and can break with small changes in the web page's structure. Here's a Selenium XPath example for how you might use an absolute XPath in your Selenium code. Suppose you have the following HTML structure: <html> <body> <div> <p>Hello, world!</p> </div> </body> </html> Absolute XPath(1/2) Copyright © 2023 HeadSpin, Inc. All Rights Reserved.
  • 6. Copyright © 2023 HeadSpin, Inc. All Rights Reserved. To find the <p> tag using an absolute XPath, you would start at the root <html> tag and provide the full path to the <p> tag: In this case, the XPath "/html/body/div/p" represents the absolute path from the root <html> tag to the desired <p> tag. In several scenarios, however, absolute XPath is not recommended unless necessary because it's brittle, and any change in the HTML structure may cause your test to fail. WebElement paragraph = driver.findElement(By.xpath("/html/body/div/p")); Absolute XPath(2/2)
  • 7. Relative XPath(1/2) It starts from any node and ends with the node you want to select. It's more flexible and preferred in most cases, as it's not affected by changes in other parts of the HTML structure. A relative XPath allows you to locate elements starting from any location within the HTML document, not just the root. The relative XPath expression usually starts with //. Here's an example of using relative XPath in Selenium with Python: ```python from selenium import webdriver # Create a new instance of the Firefox driver driver = webdriver.Firefox() # Navigate to a website driver.get("https://example.com") # Find an element using relative XPath element = driver.find_element_by_xpath("//div[@id='myDiv']/p[2]") # Perform actions on the element element.click() # Close the browser driver.quit( Copyright © 2023 HeadSpin, Inc. All Rights Reserved.
  • 8. Copyright © 2023 HeadSpin, Inc. All Rights Reserved. In the example mentioned in the previous slide , we first import the necessary modules from the Selenium library. Then, we create a new instance of the Firefox driver and navigate to a website (in this case, "https://example.com"). Next, we find an element using a relative XPath expression. The XPath used in this example selects the second `<p>` element inside a `<div>` element with the id "myDiv". You can modify the XPath expression to suit your specific needs. After finding the element, you can perform various actions on it, such as clicking it, entering text, or retrieving its attributes. Finally, we close the browser using the `quit()` method to clean up and release the resources used by the driver. Using relative XPaths is generally recommended over absolute XPaths because they are more resilient to page structure changes. Relative XPath(2/2)
  • 9. Types of XPath locators This locator allows you to identify an element by its id attribute. XPath locator by ID This locator can identify elements based on their class attribute. XPath locator by class name XPath locator by tag name This locator can identify elements by their HTML tag name. This locator identifies elements by their name attribute. XPath locator by name XPath locator by text This locator identifies elements based on their inner text. XPath locator using contains This locator can identify elements based on a substring of one of their attribute values. XPath locator using starts-with This locator identifies elements whose attribute values start with a particular string. Path locator using ends-with This locator can identify elements whose attribute values end with a particular string. Copyright © 2023 HeadSpin, Inc. All Rights Reserved.
  • 10. What is chained XPath in Selenium? Chained XPath in Selenium is a concept where multiple XPaths are used in conjunction to locate an element that might not be uniquely identifiable by a single XPath expression. In other words, instead of writing one absolute XPath, we can separate it into multiple relative XPaths. This approach can be specifically useful when dealing with complex or dynamic web structures where elements are not easily accessible through single, unique identifiers. Chaining XPaths can provide more precision and robustness in element location strategy, thus making the automation scripts more stable. Copyright © 2023 HeadSpin, Inc. All Rights Reserved.
  • 11. What are XPath Axes? Following Ancestor Child Parent XPath Axes are used for finding dynamic elements when normal XPath element search methods like name, ID, class name, etc., aren't possible. XPath Axes navigate through elements in the XML structure of a webpage. They allow you to locate elements based on their relationship with other elements, like parent, sibling, child, ancestor, or descendant. Here are few XPath methods: Copyright © 2023 HeadSpin, Inc. All Rights Reserved.
  • 12. Copyright © 2023 HeadSpin, Inc. All Rights Reserved. How HeadSpin helps streamline Selenium utilization HeadSpin offers a robust AI-driven testing Platform with easy integration with multiple automation frameworks to improve testing efficiency and ship faster to the market. 1 Support for Selenium Webdriver 2 Parallel Testing 3 Integration with CI/CD pipelines 4 Integration with CI/CD pipelines 4 Integration with CI/CD pipelines 4 Integration with CI/CD pipelines
  • 13. Conclusion XPath is critical in Selenium testing, offering a robust method for locating elements within the web page's DOM. Understanding and utilizing XPath's capabilities can greatly improve the effectiveness and efficiency of your Selenium tests. Moreover, with platforms like HeadSpin, you can enhance your testing capabilities further, leveraging its unique features aligned with Selenium standards. Reference Link: https://www.headspin.io/blog/using-xpath-in-selenium-effectively Copyright © 2023 HeadSpin, Inc. All Rights Reserved.