Tips and Tricks for XPath Expressions#
XPath is a powerful tool that allows you to filter and select nodes on an HTML page. The right approach to XPath can make your life even easier when doing web scraping and browser automation.
Find examples for both beginners and advanced users in this section.
How to Target HTML Attributes#
When targeting elements in an HTML document, it's often best to use attributes like id
, name
, or type
to make your XPath expressions more robust. This is especially useful when the HTML layout changes, but the attributes remain constant.
Let's imagine a registration form on a web page where we want to target the <input>
elements:
Instead of targeting the index //input[1]
, //input[2]
, etc., we can be more specific and target the id
attributes:
Similarly, we can also target other attributes like name
:
This will often make your code more stable if the HTML layout changes while the name
and id
attributes often remain constant. Similarly, the submit button is easily located by the type
attribute:
Example#
All in all, how to apply this for web scraping and browser automation using Browserist: