Day 22: XPath Practice & Assignments
Comprehensive XPath practice session with real-world assignments and hands-on exercises
Today's Learning Objectives
XPath Mastery
- • Apply all XPath techniques learned
- • Practice with real websites
- • Complex element location scenarios
- • XPath optimization strategies
Practical Assignments
- • Dynamic element handling
- • Table data extraction
- • Form element identification
- • Complex DOM navigation
XPath Techniques Review
Basic XPath Methods
Single Attribute
//input[@id='username']
AND Operator
//input[@type='text' and @name='user']
OR Operator
//input[@id='user' or @name='username']
Text Method
//button[text()='Submit']
Advanced XPath Methods
Contains Method
//div[contains(@class,'btn-primary')]
Starts-with Method
//input[starts-with(@id,'user')]
Normalize-space
//span[normalize-space()='Login']
XPath Axes
//div/child::input
Practice Assignments
Assignment 1: Form Element Location
Practice locating various form elements using different XPath techniques.
Website: Registration Form
Use any registration form website to practice these XPath expressions:
Task 1: Locate Username Field
Using ID: //input[@id='username']
Using Name: //input[@name='username']
Using Type: //input[@type='text']
Task 2: Locate Submit Button
Using Text: //button[text()='Submit']
Using Contains: //button[contains(text(),'Submit')]
Using Type: //input[@type='submit']
Assignment 2: Table Data Extraction
Practice extracting data from HTML tables using XPath axes and methods.
Website: https://rahulshettyacademy.com/AutomationPractice/
Task 1: Course Prices
Selenium courses: //td[contains(text(),'Selenium')]/following-sibling::td
Python courses: //td[contains(text(),'Python')]/following-sibling::td
Task 2: Employee Data
Engineer salary: //td[text()='Engineer']/following-sibling::td[2]
Chennai employees: //td[text()='Chennai']/preceding-sibling::td[2]
Assignment 3: Dynamic Elements
Handle dynamic elements where attributes change frequently.
Dynamic Element Strategies
Partial Attribute Match
//div[contains(@id,'dynamic')]
Multiple Attributes
//input[@type='text' and contains(@class,'form-control')]
Text-based Location
//span[normalize-space(text())='Click Me']
Assignment Documents
Download Practice Assignments
Complete the following assignments to master XPath techniques and element location strategies.
Assignment Document
Detailed XPath practice exercises with step-by-step instructions.
Word Document
Editable assignment document for practice and notes.
XPath Best Practices
✅ Do's
- •Use unique attributes like ID when available
- •Prefer shorter, more readable XPath expressions
- •Use contains() for partial matches
- •Test XPath in browser console first
- •Use normalize-space() for text with whitespace
❌ Don'ts
- •Avoid absolute XPath (starts with /html)
- •Don't rely on position-based selectors
- •Avoid overly complex nested expressions
- •Don't use dynamic attributes that change
- •Avoid XPath with hardcoded index values
Knowledge Check
Which XPath expression correctly locates an element with partial class name 'btn-primary'?
Key Points Summary
XPath Mastery
- • Practice all 21 XPath techniques regularly
- • Use browser console to test XPath expressions
- • Combine multiple methods for complex scenarios
- • Always prefer unique and stable attributes
Assignment Focus
- • Complete all practice assignments thoroughly
- • Test XPath on different websites
- • Document your learning and challenges
- • Prepare for advanced Selenium topics