Selenium Guide: How To Find Elements by XPath
Finding elements by XPath is a fundamental skill in Selenium automation, allowing developers and testers to precisely locate and interact with specific elements on a web page.
Once you have a solid grasp of how XPath works, you can find pretty much any element on any page regardless of the element selection that your scraper supports.
So, in this guide we will walk you through find elements on a page by XPath in Selenium:
- What is Selenium?
- What is Xpath?
- Why Should You Choose to Find By Xpath?
- How to Find Elements By Xpath in Selenium
- Real World Scraping By XPath
- Conclusion
- More Cool Articles
Need help scraping the web?
Then check out ScrapeOps, the complete toolkit for web scraping.
What is Selenium?
Selenium is a scraping framework that uses a browser. With Selenium, we can handle dynamic web elements and simulate an end user on a webpage.
To use Selenium, you need a web browser and a webdriver (this is used to control your browser). Make sure your driver version matches your Chrome version.
For example, if you have Google Chrome 119.0.6045.105
, you should install Chromedriver with the same version number. You can check your version of Chrome with the following command:
google-chrome --version
Once you have your webdriver installed, you can install Selenium using pip
.
pip install selenium
Now that Selenium has been installed, you're all set to get started scraping some content from the web.
To test our installation, we'll make a relatively simple Python script. Create a new Python file and add the following code:
from selenium import webdriver
from selenium.webdriver.common.by import By
#open an instance of Chrome
driver = webdriver.Chrome()
#navigate to a webpage
driver.get("https://quotes.toscrape.com")
#save the title of the page as a variable
title = driver.title
#print the title of the page
print(f"Page title: {title}")
#close the browser
driver.quit()
Selenium give us a wonderful toolset for finding different elements on any page including:
- NAME
- ID
- TAG_NAME
- CLASS_NAME
- CSS_SELECTOR
- XPATH
While there is a ton of power and flexibility in all of the methods above, this article focuses primarily on finding elements via XPath
.
What is XPath?
XPath is an extremely powerful tool for locating elements because it gives us a way to find literally anything on the page relatively quickly. While it is designed for XML, HTML documents also conform to the XPath standard.
Every web document has a structure, and because of that structure, there is a path, or more precisely, an XPath
to every single element on a webpage.
This structured approach gives us the power to traverse a document, find elements based on parent or more precisely ancestor
elements.
Not only is there an absolute path to every element on the page, there is also a relative
path to each element.
Elements on the page are each given a number, so in the event that elements change on the page, we can find an element, save its location, and then check the same location later on to see if it has changed!
Why Should You Choose to Find by XPath?
When finding elements by XPath, you get far more control over which elements you'd like to find.
You can filter objects on the page by basically any criteria you would ever want such as:
- By HTML tag
- By Id
- By CSS Selector
- By Text
- By Ancestor or Descendant
Brief Overview Of The DOM
The Document Object Model or "DOM" for short determines the hierarchical structure of an HTML page. Let's look at some HTML page to see how it is laid out. Take a look at the HTML below:
If you look, our parent element <html>
is laid out at the top and to the left of the document.
- If you look at the bottom, you will find the tag
</html>
which closes the<html>
element and specifies the end of our HTML content. - Between these tags, you will notice other tags, child elements of this
<html>
tag.
Now, you can take a look at the <head>
tag.
- Similar to the
<html>
tag, it is closed with a</head>
tag. * Since the<head>
content is a part of the<html>
content, it the<html>
tag is considered a parent element of the<head>
tag. - Inside the
<head>
tag, we have a<title>
tag. - So the
<title>
is a child of the parent element,<head>
and<head>
is a child element of the parent<html>
.
Understanding the XPath Syntax and Operators
Let's take another look at the XPath we copied earlier to see how XPath syntax is laid out. While it may look scary, once you understand the structure, it is actually very straightforward.
/html/body/div/div[1]/div[1]/h1/a
/html
specifies that we want to find children ordescendants
of the<html>
tag./body
specifies that we want children of any<body>
tags that are children of the<html>
tag/div
says we want<div>
elements that descend from the previous two tags/div[1]/div[1]
means that we want the first child of the first child of the previously mentioned<div>
tag/h1
specifies that we want the header, or<h1>
matching all the criteria we've used to filter thus far- Finally,
/a
specifies that we want the<a>
tag meeting all the criteria that we've specified up to this point
As you probably noticed in the example above, we used the /
operator to specify a specific root
element. If we want to find elements regardless of their location, we can use the //
operator.
For instance, if we wanted to find all <a>
elements on the page, we could instead specify //a
as opposed to the long XPath we specified above.
XPath Nodes
Let's take a look at the HTML code again that we looked at previously when we learned about the DOM.
Let's now convert this layout into a tree of nodes
so we can better understand both how they are structured both in HTML and more specifically XPath.
The figure below shows this information as a tree:
In the figure above, you should notice the following:
- The root node
html
(light blue) is the ancestor to everything on the page - Our root node has two children,
head
andbody
(blue) - The
head
node has only one child, thetitle
node (light yellow) - The
body
node has two children,div
anddiv
(light yellow) - The
div
elements also have two children each,h1
andp
(yellow)
In most webpages, there is almost always a root html
ancestor, with at least two children, head
and body
.
- The head usually contains information such as the
title
andmeta
which often contain information relevant to the initial setup and display of the page. - The
body
node is most often an ancestor of any information that is actually displayed on the page.
Xpath Elements
When we are selecting XPath elements, we can use the following operators:
head
would select all thehead
elements on the page we looked at above/head
would select all children that descend from the root node,head
.
can be used to select the current node...
selects the parent of the current node, (if you are familiar with using terminal/shell commands, you may remember thatcd ..
is used to move into theparent
of the current directory, in Xpath, it is to select theparent
of the current node)@
is used to select a node with a specific attribute for instance:[@class='quote']
XPath Predicates
Predicates are used to select a node containing a certain value. This makes finding elements of a certain type very easy.
We can use this to find pretty much anything containing CSS.
-
If we are looking for all elements of the
quote
class, we would use the predicate:[@class='quote']
. -
If we want to select all
div
elements of classquote
, we could use//div[@class='quote']
. -
If we want to find all the tag elements of the quotes, we could use
//div[@class='tag']
. -
If we want to find all elements with the link
inspirational
we could use//*[@href='/tag/inspirational']
. -
//div[@class='quote]
selects alldiv
elements with theclass
ofquote
-
//div[@class='tag']
would select alldiv
elements of theclass
:tag
-
//*[@href='/tag/inspirational']
selects all elements on the page that with thehref
(link) attribute to/tag/inspirational
How to Find Elements by XPath in Selenium
To get started finding something by XPath, you can simply right-click the object on a page inside a regular browser and choose the inspect
option.
From there, you can actually copy the XPath directly from your browser.
After copying the XPath, you can even paste it directly into your Python script!
/html/body/div/div[1]/div[1]/h1/a
We'll take a more detailed look at this in the next section.