How to find Element by XPath in Selenium Python?

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.

Examples

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 first div element using find_element() method and XPath = '/html/body/div[1]'.

The XPath expression '/html/body/div‘ returns all the immediate child divs inside body. And [1] returns the first of those elements.

HTML webpage

We shall load the following webpage at URL: https://pythonexamples.org/tmp/selenium/index-25.html.

<html>
 <body>
  <p>Hello World!</p>
  <div>Read More</div>
  <div>Go Back</div>
 </body>
</html>
Copy

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('https://pythonexamples.org/tmp/selenium/index-25.html')

# Find element by XPath expression
my_div = driver.find_element(By.XPATH, '/html/body/div[1]')
print(my_div.text)

# Close the driver
driver.quit()

Output

Read More

2. There are multiple elements identified 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'.

The XPath expression '//div' matches all the divs in the given webpage.

Even through there are multiple div elements, find_element() returns only the first element of those.

HTML webpage

We shall load the following webpage at URL: https://pythonexamples.org/tmp/selenium/index-26.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>
Copy

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('https://pythonexamples.org/tmp/selenium/index-26.html')

# Find element by XPath expression
my_div = driver.find_element(By.XPATH, '//div')
print(my_div.text)

# Close the driver
driver.quit()

Output

Read More 1

3. There is 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.

HTML webpage

<html>
  <head></head>
  <body>
    <h2>Hello User!</h2>
    <p id="para1" class="message intro">This is my <strong>first</strong> paragraph. This is a sample text. Welcome!</p>
  </body>
</html>
Copy

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('https://pythonexamples.org/tmp/selenium/index-28.html')

# Find element by XPath expression
my_div = driver.find_element(By.XPATH, '//div')
print(my_div.text)

# Close the driver
driver.quit()

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.

Related Tutorials

Quiz on Selenium

Q1. What is Selenium Python used for?

Not answered

Q2. What is a web driver in Selenium Python?

Not answered

Q3. What is the method used to interact with web elements in Selenium Python?

Not answered

Q4. What is the method used to switch to a different frame in Selenium Python?

Not answered

Q5. What is the method used to select a value from a drop-down list in Selenium Python?

Not answered
Code copied to clipboard successfully 👍