Techniques to find XPath in Selenium WebDriver

Table of contents

Introduction:

XPath is a powerful tool for locating elements in Selenium WebDriver. In this blog post, we will explore advanced techniques for constructing XPath expressions to effectively locate elements on web pages. We will cover a wide range of scenarios and provide practical examples using Java and Selenium.


1. Selecting Elements by ID

XPath: "//*[@id='elementID']"

Example Usage:

WebElement element = driver.findElement(By.xpath("//*[@id='elementID']"));

2. Selecting Elements by Class

XPath: "//*[@class='className']"

Example Usage:

List<WebElement> elements = driver.findElements(By.xpath("//*[@class='className']"));

3. Selecting Elements by Attribute Value

XPath: "//*[@attribute='value']"

Example Usage:

WebElement element = driver.findElement(By.xpath("//*[@attribute='value']"));

4. Selecting Elements by Attribute Name and Value

XPath: "//tagName[@attribute='value']"

Example Usage:

WebElement element = driver.findElement(By.xpath("//tagName[@attribute='value']"));

5. Selecting Elements by Attribute Name and Partial Value

XPath: "//tagName[contains(@attribute, 'partialValue')]"

Example Usage:

WebElement element = driver.findElement(By.xpath("//tagName[contains(@attribute, 'partialValue')]"));

6. Selecting Elements by Attribute Name and Starting Value

XPath: "//tagName[starts-with(@attribute, 'startValue')]"

Example Usage:

WebElement element = driver.findElement(By.xpath("//tagName[starts-with(@attribute, 'startValue')]"));

7. Selecting Elements by Attribute Name and Ending Value

XPath: "//tagName[ends-with(@attribute, 'endValue')]"

Example Usage:

WebElement element = driver.findElement(By.xpath("//tagName[ends-with(@attribute, 'endValue')]"));

8. Selecting Elements by Text Content

XPath: "//tagName[text()='textValue']"

Example Usage:

WebElement element = driver.findElement(By.xpath("//tagName[text()='textValue']"));

9. Selecting Elements with a Specific Index

XPath: "(//tagName)[index]"

Example Usage:

WebElement element = driver.findElement(By.xpath("(//tagName)[index]"));

10. Selecting the First Occurrence of an Element

XPath: "(//tagName)[1]"

Example Usage:

WebElement element = driver.findElement(By.xpath("(//tagName)[1]"));

11. Selecting the Last Occurrence of an Element

XPath: "(//tagName)[last()]"

Example Usage:

WebElement element = driver.findElement(By.xpath("(//tagName)[last()]"));

12. Selecting Parent Elements

XPath: "//tagName[@attribute='value']/parent::parentTagName"

Example Usage:

WebElement parentElement = driver.findElement(By.xpath("//tagName[@attribute='value']/parent::parentTagName"));

13. Selecting Child Elements

XPath: "//parentTagName[@attribute='value']/child::childTagName"

Example Usage:

List<WebElement> childElements = driver.findElements(By.xpath("//parentTagName[@attribute='value']/child::childTagName"));

14. Selecting Preceding Sibling Elements

XPath: "//tagName[@attribute='value']/preceding-sibling::siblingTagName"

Example Usage:

List<WebElement> siblingElements = driver.findElements(By.xpath("//tagName[@attribute='value']/preceding-sibling::siblingTagName"));

15. Selecting Following Sibling Elements

XPath: "//

tagName[@attribute='value']/following-sibling::siblingTagName"

Example Usage:

List<WebElement> siblingElements = driver.findElements(By.xpath("//tagName[@attribute='value']/following-sibling::siblingTagName"));

16. Selecting Elements with a Specific Attribute

XPath: "//tagName[@attribute]"

Example Usage:

List<WebElement> elements = driver.findElements(By.xpath("//tagName[@attribute]"));

17. Selecting Elements without a Specific Attribute

XPath: "//tagName[not(@attribute)]"

Example Usage:

List<WebElement> elements = driver.findElements(By.xpath("//tagName[not(@attribute)]"));

18. Selecting Elements with an Attribute Containing a Value

XPath: "//[@attribute\='value']"

Example Usage:

List<WebElement> elements = driver.findElements(By.xpath("//*[@attribute*='value']"));

19. Selecting Elements with an Attribute Starting with a Value

XPath: "//*[@attribute[starts-with(., 'value')]]"

Example Usage:

List<WebElement> elements = driver.findElements(By.xpath("//*[@attribute[starts-with(., 'value')]]"));

20. Selecting Elements with an Attribute Ending with a Value

XPath: "//*[@attribute[substring(@attribute, string-length(@attribute) - string-length('value') + 1)='value']]"

Example Usage:

List<WebElement> elements = driver.findElements(By.xpath("//*[@attribute[substring(@attribute, string-length(@attribute) - string-length('value') + 1)='value']]"));

21. Selecting Elements Based on Position

XPath: "(//tagName)[position()=X]"

Example Usage:

WebElement element = driver.findElement(By.xpath("(//tagName)[position()=X]"));

22. Selecting Elements Based on Ancestors

XPath: "//ancestorTagName//tagName"

Example Usage:

List<WebElement> elements = driver.findElements(By.xpath("//ancestorTagName//tagName"));

23. Selecting Elements Based on Descendants

XPath: "//parentTagName/descendant::tagName"

Example Usage:

List<WebElement> elements = driver.findElements(By.xpath("//parentTagName/descendant::tagName"));

24. Selecting Elements Based on Preceding Elements

XPath: "//precedingTagName/following-sibling::tagName"

Example Usage:

List<WebElement> elements = driver.findElements(By.xpath("//precedingTagName/following-sibling::tagName"));

25. Selecting Elements Based on Following Elements

XPath: "//followingTagName/preceding-sibling::tagName"

Example Usage:

List<WebElement> elements = driver.findElements(By.xpath("//followingTagName/preceding-sibling::tagName"));

26. Selecting Elements with Specific Text Content Ignoring Leading/Trailing Spaces

XPath: "//tagName[normalize-space(text())='textValue']"

Example Usage:

WebElement element = driver.findElement(By.xpath("//tagName[normalize-space(text())='textValue']"));

27. Selecting All Elements

XPath: "//*"

Example Usage:

List<WebElement> elements = driver.findElements(By.xpath("//*"));

28. Selecting Elements with Attribute Containing a Value (Wildcard)

XPath: "//[@attribute\='value']"

Example Usage:

List<WebElement> elements = driver.findElements(By.xpath("//*[@attribute*='value']"));

29. Selecting Elements with Attribute Starting with a Value (Dynamic)

XPath: "//*[@attribute[starts-with(., '

dynamicValue')]]"

Example Usage:

List<WebElement> elements = driver.findElements(By.xpath("//*[@attribute[starts-with(., 'dynamicValue')]]"));

30. Selecting Elements by Passing Variables (Dynamic)

XPath: "//*[@attribute='$variable']" (Replace '$variable' with the actual variable name)

Example Usage:

String variableValue = "dynamicValue";
WebElement element = driver.findElement(By.xpath("//*[@attribute='" + variableValue + "']"));

31. Selecting Elements with Dynamic Text Content (Dynamic)

XPath: "//tagName[contains(text(), $dynamicText)]" (Replace '$dynamicText' with the actual dynamic text value)

Example Usage:

String dynamicText = "dynamicValue";
WebElement element = driver.findElement(By.xpath("//tagName[contains(text(), '" + dynamicText + "')]"));

32. Selecting Elements with Dynamic Index Value (Dynamic)

XPath: "(//tagName)[$dynamicIndex]" (Replace '$dynamicIndex' with the actual dynamic index value)

Example Usage:

int dynamicIndex = 2;
WebElement element = driver.findElement(By.xpath("(//tagName)[" + dynamicIndex + "]"));

Conclusion:

XPath expressions provide a versatile and flexible approach to locate elements in Selenium WebDriver. By mastering these advanced techniques, you can effectively handle complex scenarios and efficiently interact with elements on web pages. Experiment with these examples in your Selenium projects to improve your test automation capabilities.