How to find Elements by XPath using Selenium?
Find Elements by XPath
To find the HTML Elements by an XPath (language used for locating nodes in HTML) using Selenium in Python, call find_elements()
method and pass By.XPATH
as the first argument, and the XPath value as the second argument.
find_elements(By.XPATH, "xpath_value")
find_elements()
method returns all the HTML Elements, that satisfy the given XPath value, as a list.
If there are no elements in the document for the given XPath value, then find_elements()
method returns an empty list.
Example
Consider the HTML document at the URL /tmp/selenium/index-55.html, with the following HTML content.
HTML Webpage
<html>
<body>
<h2>Hello World</h2>
<p>This is a paragraph.</p>
<div name="xyz">Div 1</div>
<div name="abc">Div 2</div>
</body>
</html>
In the following Python program, we will find all the div
elements in the document using find_elements()
method using the XPath value '/html/body/div'
.
Python Program (Selenium)
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
# Navigate to the url
driver.get('/tmp/selenium/index-55.html')
# Find elements by XPath expression
my_elements = driver.find_elements(By.XPATH, '/html/body/div')
for element in my_elements:
print(element.get_attribute("outerHTML"))
# Close the driver
driver.quit()
Output
<div name="xyz">Div 1</div>
<div name="abc">Div 2</div>
Summary
In this Python Selenium tutorial, we learned how to find all the HTML elements by given XPath, in webpage, using Selenium.