Skip to content

XPath Basics 🔍#

XPath is a query language that is used for traversing through not just an HTML document – but also XML, XSLT and other document types. It is used commonly to search particular elements or attributes with matching patterns, and XPath includes over 200 built-in functions.

While we use XPath in context of HTML and Python, it's commonly used in other programming languages like JavaScript, Java, C#, C++, and many more.

How It's Used in Browserist#

XPath is an integral of Browserist to target elements of a web page. For example, we can easily get the text of the first <h1> headline element without knowinw its exact location on the page:

Python
text = browser.get.text("//h1[1]")
print(text)

Or we can easily input values in a form if we know the id attribute of the input field. Again, we don't want to know the exact location of the input field on the page:

Python
browser.input.value("//*[@id='username']", "John Doe")
browser.input.value("//*[@id='password']", "password123")