XPath: Contains
The contains() function in XPath is used to select elements whose attribute values contain a specified substring. This is particularly useful when you need to match part of a string rather than the entire value.
You can use the following XPath:
This selects list items (li) within an unordered list (ul) where the class attribute contains the substring 'list-group-item'.
Example 1: //ul[@class='list-group']/li[contains(@class,'list-group-item')]
This targets list items where the id attribute contains the substring 'list-group-item'
Example 2: //ul[@class='list-group']/li[contains(@id,'list-group-item')]
This selects list items that contain the text 'Sample Item' anywhere within the element.
Example 3: //ul[@class='list-group']/li[contains(.,'Sample Item')]
- Sample Item 0001
- Sample Item 0002
- Sample Item 0003
- Sample Item 0004
- Sample Item 0005
- Sample Item 0006
- Sample Item 0007
- Sample Item 0008
- Sample Item 0009
- Sample Item 0010