How to Use Conditional XPath Functions#
Match Elements with Specific Text#
Sometimes you can't use attributes to easily target elements. A calendar is a good example of this. Let's imagine we want to select the first day of a given month:
We can locate June 1 by using by indexing <li>1</li>
as the fourth date element: //ul[@class='dates']/li[4]
. But what happens in July, August, etc. when the first day of the month isn't a Thursday?
Let's instead use a conditional function for XPath:
The …/li[text()='1']
part will return the first exact text match of 1
(and ignore any later matches).
If you want to select other dates, why not create a function in Python that dynamically generates the XPath expression for you?
Python | |
---|---|
Tip
While the …/li[text()='1']
method locates the first exact match of 1
, it's sometimes favourable to locate the first non-exact match with either the …/li[contains(text(), '1')]
or …/li[normalize-space()='1']
methods as they handle eventual white space more graceful. Despite the differences, all of these conditional methods would yield the correct answer in the calendar case.
Learn more pattern matching techniques for text or tips for node selection in the XPath cheatsheets section.
Example#
All in all, how to apply this for web scraping and browser automation using Browserist: