Finding elements by tag name helps you locate parts of a webpage using their HTML tags, like buttons or paragraphs.
Find element by tag name in Selenium Python
element = driver.find_element(By.TAG_NAME, 'tagname') elements = driver.find_elements(By.TAG_NAME, 'tagname')
Use find_element to get the first matching element.
Use find_elements to get a list of all matching elements.
<button> element on the page.button = driver.find_element(By.TAG_NAME, 'button')<p> elements and stores them in a list.all_paragraphs = driver.find_elements(By.TAG_NAME, 'p')first_image = driver.find_element(By.TAG_NAME, 'img')This script opens a simple webpage with headings, paragraphs, and a button. It finds the first paragraph, counts all paragraphs, and finds the button, printing their texts.
from selenium import webdriver from selenium.webdriver.common.by import By # Start the browser with webdriver.Chrome() as driver: # Open a simple webpage driver.get('data:text/html,<html><body><h1>Title</h1><p>Paragraph 1</p><p>Paragraph 2</p><button>Click me</button></body></html>') # Find the first paragraph first_p = driver.find_element(By.TAG_NAME, 'p') print('First paragraph text:', first_p.text) # Find all paragraphs all_p = driver.find_elements(By.TAG_NAME, 'p') print('Number of paragraphs:', len(all_p)) # Find the button button = driver.find_element(By.TAG_NAME, 'button') print('Button text:', button.text)
Tag names are case-insensitive in HTML but use lowercase in Selenium for consistency.
If no element is found, find_element throws an error; find_elements returns an empty list.
Use find_element(By.TAG_NAME, 'tagname') to get the first element by tag.
Use find_elements(By.TAG_NAME, 'tagname') to get all elements by tag.
This method is simple and useful for common HTML tags like button, p, img, etc.
Practice
find_element(By.TAG_NAME, 'button') do in Selenium Python?Solution
Step 1: Understand the method used
The methodfind_elementreturns the first matching element found.Step 2: Understand the locator type
UsingBy.TAG_NAMEwith 'button' searches for the first <button> tag element.Final Answer:
Finds the first <button> element on the page -> Option DQuick Check:
find_element + TAG_NAME = first matching tag element [OK]
- Confusing find_element with find_elements
- Using class name or ID instead of tag name
- Expecting all elements instead of first only
p elements using Selenium Python?Solution
Step 1: Identify method for multiple elements
To get all matching elements, usefind_elements(plural).Step 2: Use correct locator for tag name
UseBy.TAG_NAMEwith the tag string 'p'.Final Answer:
driver.find_elements(By.TAG_NAME, 'p') -> Option CQuick Check:
find_elements + TAG_NAME = all matching tags [OK]
- Using find_element instead of find_elements
- Using deprecated find_element_by_tag_name syntax
- Confusing class name locator with tag name
<div> <button>Click</button> <button>Submit</button> </div>
What will
driver.find_element(By.TAG_NAME, 'button').text return?Solution
Step 1: Understand find_element behavior
It returns the first matching element by tag name, which is the first <button> with text "Click".Step 2: Access the text property
The.textattribute returns the visible text inside that element, which is "Click".Final Answer:
"Click" -> Option BQuick Check:
First button text = "Click" [OK]
- Assuming it returns all buttons' text combined
- Confusing find_element with find_elements
- Expecting an exception when element exists
element = driver.find_elements(By.TAG_NAME, 'img')
But you get an error when trying
element.click(). What is the problem?Solution
Step 1: Understand return type of find_elements
find_elements returns a list of WebElements, not a single element.Step 2: Identify why click() fails
Callingclick()on a list causes an error because lists don't have click method.Final Answer:
find_elements returns a list, not a single element -> Option AQuick Check:
find_elements = list, cannot call click() directly [OK]
- Calling click() on list instead of element
- Confusing find_element and find_elements return types
- Assuming all tags support click() without checking
button on a page. Which code correctly finds and clicks it using tag name?Solution
Step 1: Use find_elements to get all buttons
Usefind_elements(By.TAG_NAME, 'button')to get a list of all button elements.Step 2: Access second button and click
Index 1 accesses the second button (0-based index), then callclick()on it.Final Answer:
buttons = driver.find_elements(By.TAG_NAME, 'button') buttons[1].click() -> Option AQuick Check:
find_elements + index + click = correct usage [OK]
- Using find_element and indexing it like a list
- Calling click() on list instead of element
- Using wrong index (like 2 instead of 1 for second element)
Ready to prove it?
Pass the test and this pattern moves unknown
