Contents
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")
Scenario 1: HTML page with single element by given ID
In the following example, we have an HTML page with a div
element whose id="xyz"
.
index.html
<html>
<body>
<p>Hello World!</p>
<div id="xyz">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 id
myDiv = driver.find_element(By.ID, 'xyz')
print(myDiv)
print(myDiv.text)
Output
<selenium.webdriver.remote.webelement.WebElement (session="62c57767d8205c7ef6324359c2e74675", element="0246000a-73d1-426a-be39-60d97be601b9")>
Read More
Scenario 2: HTML page with 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.
If find_element()
function finds more than one element by the given id
, then it returns only the first element.
index.html
<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>
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 id
myDiv = driver.find_element(By.ID, 'xyz')
print(myDiv)
print(myDiv.text)
Output
<selenium.webdriver.remote.webelement.WebElement (session="419b556f9fad1b755f5a55417fff42eb", element="4bba392c-46e3-4ad1-a101-56bdd35a9753")>
Read More 1
Scenario 3: HTML page with no element by given ID
In the following example, we have an HTML page with no element whose id="xyz"
.
If find_element()
function finds no element by the given id
, then it raises NoSuchElementException
.
index.html
<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>
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 id
myDiv = driver.find_element(By.ID, 'xyz')
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":"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.