How to Find Element by ID in Selenium Python?

Find Element by ID

To find an HTML Element by id attribute using Selenium in Python, call find_element() method and pass By.ID as the first argument, and the id attribute’s value ((of the HTML Element we need to find)) as the second argument.

find_element(By.ID, "id_value")

Examples

1. There is only one element by given ID

In this example, we shall consider loading the webpage at URL: https://pythonexamples.org/tmp/selenium/index-25.html. The contents of this webpage is given below. The webpage contains a div element with id="xyz".

<html>
 <body>
  <p>Hello World!</p>
  <div id="xyz">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 id
my_div = driver.find_element(By.ID, 'xyz')
print(my_div.get_attribute('outerHTML'))

# Close the driver
driver.quit()

Output

<div id="xyz">Read More</div>

2. There are multiple elements by given ID

In the following example, we have an HTML page with two div elements whose id="xyz". Even though we cannot have multiple elements with the same id, this scenario is quite possible.

In this example, we shall consider loading the webpage at URL: https://pythonexamples.org/tmp/selenium/index-26.html. The contents of this webpage is given below. There are two div elements with the same id="xyz".

Even if there are more than one element by the given id, find_element() function returns only the first element for the given selection criteria.

<html>
 <body>
  <p>First Article</p>
  <div id="xyz">Read More 1</div>
  <div>Go Back</div>
  <br>
  <p>Second Article</p>
  <div id="xyz">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 id
my_div = driver.find_element(By.ID, 'xyz')
print(my_div.get_attribute('outerHTML'))

# Close the driver
driver.quit()

Output

<div id="xyz">Read More 1</div>

3. There is no element by given ID

In this example, we shall consider loading the webpage at URL: https://pythonexamples.org/tmp/selenium/index-27.html. The contents of this webpage is given below. The webpage contains no div element with id="xyz".

If find_element() function finds no element by the given id, then it raises NoSuchElementException.

<html>
 <body>
  <p>First Article</p>
  <div>Read More 1</div>
  <div>Go Back</div>
  <br>
  <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-27.html')

# Find element by id
my_div = driver.find_element(By.ID, 'xyz')
print(my_div.get_attribute('outerHTML'))

# 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":"css selector","selector":"[id="xyz"]"}

Summary

In this tutorial of Python Examples, we learned how to find an element by id attribute 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 navigate back to the previous page in Selenium Python?

Not answered

Q4. What is the method used to take a screenshot 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 👍