Contents
Find Element by Tag Name
To find an HTML Element by tag name using Selenium in Python, call find_element()
method and pass By.TAG_NAME
as the first argument, and the tag name (of the HTML Element we need to find) as the second argument.
find_element(By.TAG_NAME, "tag_name_value")
If there are multiple HTML Elements with the same given tag name, then find_element()
returns the first HTML Element of those.
Scenario 1: HTML page with single element by given tag name
In the following example, we have an HTML page with a div
element. In the Python program, we will find the HTML element whose tag name is 'div'
, using find_element()
method, and print that element to the console.
index.html
<html>
<body>
<p>This is a sample paragraph.</p>
<div>Hello World!</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 tag name
myDiv = driver.find_element(By.TAG_NAME, 'div')
print(myDiv.get_attribute("outerHTML"))
Output
<div>Hello World!</div>
Scenario 2: HTML page with multiple elements by given tag name
Now, let us consider the scenario where there are multiple HTML Elements with the given tag name. We will use find_element()
function to find the first element whose tag name is 'div'
.
Even through there are two elements with the same given tag name, find_element()
returns only the first element of those.
index.html
<html>
<body>
<p>This is a sample paragraph.</p>
<div>Hello World!</div>
<div>Welcome.</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 tag name
myDiv = driver.find_element(By.TAG_NAME, 'div')
print(myDiv.get_attribute("outerHTML"))
Output
<div>Hello World!</div>
Scenario 3: HTML page with no element by given tag name
If there is no element by given tag name, 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 tag name 'div'
, using find_element()
function, the function throws NoSuchElementException
.
index.html
<html>
<body>
<p>This is a sample paragraph.</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 tag name
myDiv = driver.find_element(By.TAG_NAME, 'div')
print(myDiv.get_attribute("outerHTML"))
Output
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"tag name","selector":"div"}
Summary
In this tutorial of Python Examples, we learned how to find an element by tag name in webpage, using Selenium.