Contents
Find Element by XPath
To find an HTML Element by an XPath (language used for locating nodes in HTML) using Selenium in Python, call find_element()
method and pass By.XPATH
as the first argument, and the XPath value as the second argument.
find_element(By.XPATH, "xpath_value")
If there are multiple HTML Elements with the same given XPath value in the webpage, then find_element() returns the first HTML Element of those.
Scenario 1: Get First Div in the Body
In the following example, we have an HTML page with a div
element. In the Python program, we will find the div
element using find_element()
method and XPath = '/html/body/div[1]'
.
index.html
<html>
<body>
<p>Hello World!</p>
<div>Read More</div>
<div>Go Back</div>
</body>
</html>
Python Program (Selenium)
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import By
service = Service(executable_path="/usr/local/bin/chromedriver")
#initialize web driver
with webdriver.Chrome(service=service) as driver:
#navigate to the url
driver.get('http://127.0.0.1:5500/localwebsite/index.html')
#find element by xpath
myDiv = driver.find_element(By.XPATH, '/html/body/div[1]')
print(myDiv)
print(myDiv.text)
Output
<selenium.webdriver.remote.webelement.WebElement (session="cb3cf04980564db011ce9d6ebd0ef82e", element="fbb2d26a-1cfb-48af-8a18-f554dc86e355")>
Read More
Scenario 2: HTML page with multiple elements by given XPath
Now, let us consider the scenario where there are multiple HTML Elements that match the given XPath. We will use find_element()
function to find the first element which matches with the given XPath '//div'
.
Even through there are multiple div
elements, find_element()
returns only the first element of those.
index.html
<html>
<body>
<p>First Article</p>
<div>Read More 1</div>
<div>Go Back</div>
<p>Second Article</p>
<div>Read More 2</div>
<div>Go Back</div>
</body>
</html>
Python Program (Selenium)
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import By
service = Service(executable_path="/usr/local/bin/chromedriver")
#initialize web driver
with webdriver.Chrome(service=service) as driver:
#navigate to the url
driver.get('http://127.0.0.1:5500/localwebsite/index.html')
#find element by xpath
myDiv = driver.find_element(By.XPATH, '//div')
print(myDiv)
print(myDiv.text)
Output
<selenium.webdriver.remote.webelement.WebElement (session="b5a43ce51bea10f1438b4dfec969b559", element="465d82ec-4655-4276-bdc7-7fb0dd89572b")>
Read More 1
Scenario 3: HTML page with no element by given XPath
If there is no element by given XPath, find_element() function raises NoSuchElementException
.
In the following example, we have taken an HTML page with no div elements. When we try to find element by the XPath='//div'
using find_element()
function, then the function throws NoSuchElementException
.
index.html
<html>
<body>
<p>First Article</p>
<p>Second Article</p>
</body>
</html>
Python Program (Selenium)
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import By
service = Service(executable_path="/usr/local/bin/chromedriver")
#initialize web driver
with webdriver.Chrome(service=service) as driver:
#navigate to the url
driver.get('http://127.0.0.1:5500/localwebsite/index.html')
#find element by xpath
myDiv = driver.find_element(By.XPATH, '//div')
print(myDiv)
print(myDiv.text)
Output
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div"}
Summary
In this tutorial of Python Examples, we learned how to find an element by given XPath in webpage, using Selenium.