How To Iterate Through With Selenium In Python?
Introduction
Selenium is a popular tool for automating web browsers, and Python is a versatile language for web scraping and automation. When working with Selenium in Python, you may encounter situations where you need to iterate through elements within a <tbody>
tag. In this article, we will explore how to achieve this using Selenium and Python.
Prerequisites
Before we dive into the solution, make sure you have the following installed:
- Python 3.x
- Selenium WebDriver for Python (install using
pip install selenium
) - A WebDriver executable (e.g., ChromeDriver or GeckoDriver)
Understanding the Problem
You are trying to iterate through a list of <tr>
elements within a <tbody>
element using Selenium in Python. However, your print statements are not returning any values, and the output indicates that the element is not found. This can be frustrating, especially when you're not sure what's going wrong.
Solution
To iterate through <tr>
elements within a <tbody>
tag, you can use the following steps:
Step 1: Locate the Element
First, you need to locate the <tbody>
element using Selenium's find_element_by_tag_name()
method. This method returns a single element that matches the specified tag name.
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://www.example.com")
tbody_element = driver.find_element(By.TAG_NAME, "tbody")
Step 2: Get the List of Elements
Once you have the <tbody>
element, you can get the list of <tr>
elements using the find_elements_by_tag_name()
method. This method returns a list of elements that match the specified tag name.
# Get the list of <tr> elements
tr_elements = tbody_element.find_elements(By.TAG_NAME, "tr")
Step 3: Iterate Through the List of Elements
Now that you have the list of <tr>
elements, you can iterate through it using a for loop. You can access each element's attributes, such as text or links, using the get_attribute()
method.
# Iterate through the list of <tr> elements
for tr_element in tr_elements:
# Get the text of the current <tr> element
text = tr_element.get_attribute("textContent")
print(text)
Example Use Case
Let's say you want to scrape the table on the Wikipedia page for the list of countries by population. You can use the following code to iterate through the <tr>
elements within the <tbody>
tag:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://en.wikipedia.org/wiki/List_of_countries_by_population_(United_Nations)")
tbody_element = driver.find_element(By.TAG_NAME, "tbody")
for tr_element in tr_elements:
# Get the text of the current <tr> element
text = tr_element.get_attribute("textContent")
print(text)
Tips and Variations
- To iterate through elements within a specific
<td>
tag, use the find_elements_by_tag_name()
method on the <td>
element.
- To access the attributes of an element, use the
get_attribute()
method.
- To handle dynamic content, use Selenium's
WebDriverWait
class to wait for the element to be present on the page.
Conclusion
Q: What is the difference between find_element_by_tag_name() and find_elements_by_tag_name()?
A: find_element_by_tag_name()
returns a single element that matches the specified tag name, while find_elements_by_tag_name()
returns a list of elements that match the specified tag name.
Q: How do I handle dynamic content when iterating through elements?
A: You can use Selenium's WebDriverWait
class to wait for the element to be present on the page. For example:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.example.com")
tbody_element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.TAG_NAME, "tbody"))
)
tr_elements = tbody_element.find_elements(By.TAG_NAME, "tr")
Q: How do I access the attributes of an element when iterating through elements?
A: You can use the get_attribute()
method to access the attributes of an element. For example:
# Iterate through the list of <tr> elements
for tr_element in tr_elements:
# Get the text of the current <tr> element
text = tr_element.get_attribute("textContent")
print(text)
Q: What is the difference between find_element_by_tag_name() and find_element_by_xpath()?
A: find_element_by_tag_name()
returns a single element that matches the specified tag name, while find_element_by_xpath()
returns a single element that matches the specified XPath expression.
Q: How do I use XPath expressions to locate elements within a tag?
A: You can use the find_element_by_xpath()
method to locate elements within a
tag using an XPath expression. For example:
# Locate the <tbody> element using an XPath expression
tbody_element = driver.find_element_by_xpath("//tbody")
tr_elements = tbody_element.find_elements(By.TAG_NAME, "tr")
Q: What is the difference between find_elements_by_tag_name() and find_elements_by_xpath()?
A: find_elements_by_tag_name()
returns a list of elements that match the specified tag name, while find_elements_by_xpath()
returns a list of elements that match the specified XPath expression.
Q: How do I use Selenium's WebDriverWait
class to wait for multiple elements to be present on the page?
A: You can use the WebDriverWait
class with the until
method to wait for multiple elements to be present on the page. For example:
from selenium import webdriver
from selenium.webdriver.common.by import
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.example.com")
tbody_element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.TAG_NAME, "tbody"))
)
tr_elements = WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located((By.TAG_NAME, "tr"))
)
Q: What are some common mistakes to avoid when iterating through elements with Selenium?
A: Some common mistakes to avoid when iterating through
elements with Selenium include:
- Not waiting for the element to be present on the page
- Not using the correct method to locate the element (e.g.,
find_element_by_tag_name()
vs. find_elements_by_tag_name()
)
- Not accessing the attributes of the element correctly (e.g., using
get_attribute()
vs. text
)
- Not handling dynamic content correctly (e.g., using
WebDriverWait
to wait for the element to be present on the page)
<tbody>
element using Selenium's find_element_by_tag_name()
method. This method returns a single element that matches the specified tag name.from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://www.example.com")
tbody_element = driver.find_element(By.TAG_NAME, "tbody")
Once you have the <tbody>
element, you can get the list of <tr>
elements using the find_elements_by_tag_name()
method. This method returns a list of elements that match the specified tag name.
# Get the list of <tr> elements
tr_elements = tbody_element.find_elements(By.TAG_NAME, "tr")
Step 3: Iterate Through the List of Elements
Now that you have the list of <tr>
elements, you can iterate through it using a for loop. You can access each element's attributes, such as text or links, using the get_attribute()
method.
# Iterate through the list of <tr> elements
for tr_element in tr_elements:
# Get the text of the current <tr> element
text = tr_element.get_attribute("textContent")
print(text)
Example Use Case
Let's say you want to scrape the table on the Wikipedia page for the list of countries by population. You can use the following code to iterate through the <tr>
elements within the <tbody>
tag:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://en.wikipedia.org/wiki/List_of_countries_by_population_(United_Nations)")
tbody_element = driver.find_element(By.TAG_NAME, "tbody")
for tr_element in tr_elements:
# Get the text of the current <tr> element
text = tr_element.get_attribute("textContent")
print(text)
Tips and Variations
- To iterate through elements within a specific
<td>
tag, use the find_elements_by_tag_name()
method on the <td>
element.
- To access the attributes of an element, use the
get_attribute()
method.
- To handle dynamic content, use Selenium's
WebDriverWait
class to wait for the element to be present on the page.
Conclusion
Q: What is the difference between find_element_by_tag_name() and find_elements_by_tag_name()?
A: find_element_by_tag_name()
returns a single element that matches the specified tag name, while find_elements_by_tag_name()
returns a list of elements that match the specified tag name.
Q: How do I handle dynamic content when iterating through elements?
A: You can use Selenium's WebDriverWait
class to wait for the element to be present on the page. For example:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.example.com")
tbody_element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.TAG_NAME, "tbody"))
)
tr_elements = tbody_element.find_elements(By.TAG_NAME, "tr")
Q: How do I access the attributes of an element when iterating through elements?
A: You can use the get_attribute()
method to access the attributes of an element. For example:
# Iterate through the list of <tr> elements
for tr_element in tr_elements:
# Get the text of the current <tr> element
text = tr_element.get_attribute("textContent")
print(text)
Q: What is the difference between find_element_by_tag_name() and find_element_by_xpath()?
A: find_element_by_tag_name()
returns a single element that matches the specified tag name, while find_element_by_xpath()
returns a single element that matches the specified XPath expression.
Q: How do I use XPath expressions to locate elements within a tag?
A: You can use the find_element_by_xpath()
method to locate elements within a
tag using an XPath expression. For example:
# Locate the <tbody> element using an XPath expression
tbody_element = driver.find_element_by_xpath("//tbody")
tr_elements = tbody_element.find_elements(By.TAG_NAME, "tr")
Q: What is the difference between find_elements_by_tag_name() and find_elements_by_xpath()?
A: find_elements_by_tag_name()
returns a list of elements that match the specified tag name, while find_elements_by_xpath()
returns a list of elements that match the specified XPath expression.
Q: How do I use Selenium's WebDriverWait
class to wait for multiple elements to be present on the page?
A: You can use the WebDriverWait
class with the until
method to wait for multiple elements to be present on the page. For example:
from selenium import webdriver
from selenium.webdriver.common.by import
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.example.com")
tbody_element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.TAG_NAME, "tbody"))
)
tr_elements = WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located((By.TAG_NAME, "tr"))
)
Q: What are some common mistakes to avoid when iterating through elements with Selenium?
A: Some common mistakes to avoid when iterating through
elements with Selenium include:
- Not waiting for the element to be present on the page
- Not using the correct method to locate the element (e.g.,
find_element_by_tag_name()
vs. find_elements_by_tag_name()
)
- Not accessing the attributes of the element correctly (e.g., using
get_attribute()
vs. text
)
- Not handling dynamic content correctly (e.g., using
WebDriverWait
to wait for the element to be present on the page)
Now that you have the list of <tr>
elements, you can iterate through it using a for loop. You can access each element's attributes, such as text or links, using the get_attribute()
method.
# Iterate through the list of <tr> elements
for tr_element in tr_elements:
# Get the text of the current <tr> element
text = tr_element.get_attribute("textContent")
print(text)
Example Use Case
Let's say you want to scrape the table on the Wikipedia page for the list of countries by population. You can use the following code to iterate through the <tr>
elements within the <tbody>
tag:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://en.wikipedia.org/wiki/List_of_countries_by_population_(United_Nations)")
tbody_element = driver.find_element(By.TAG_NAME, "tbody")
for tr_element in tr_elements:
# Get the text of the current <tr> element
text = tr_element.get_attribute("textContent")
print(text)
Tips and Variations
- To iterate through elements within a specific
<td>
tag, use thefind_elements_by_tag_name()
method on the<td>
element. - To access the attributes of an element, use the
get_attribute()
method. - To handle dynamic content, use Selenium's
WebDriverWait
class to wait for the element to be present on the page.
Conclusion
Q: What is the difference between find_element_by_tag_name() and find_elements_by_tag_name()?
A: find_element_by_tag_name()
returns a single element that matches the specified tag name, while find_elements_by_tag_name()
returns a list of elements that match the specified tag name.
Q: How do I handle dynamic content when iterating through elements?
A: You can use Selenium's WebDriverWait
class to wait for the element to be present on the page. For example:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.example.com")
tbody_element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.TAG_NAME, "tbody"))
)
tr_elements = tbody_element.find_elements(By.TAG_NAME, "tr")
Q: How do I access the attributes of an element when iterating through elements?
A: You can use the get_attribute()
method to access the attributes of an element. For example:
# Iterate through the list of <tr> elements
for tr_element in tr_elements:
# Get the text of the current <tr> element
text = tr_element.get_attribute("textContent")
print(text)
Q: What is the difference between find_element_by_tag_name() and find_element_by_xpath()?
A: find_element_by_tag_name()
returns a single element that matches the specified tag name, while find_element_by_xpath()
returns a single element that matches the specified XPath expression.
Q: How do I use XPath expressions to locate elements within a tag?
A: You can use the find_element_by_xpath()
method to locate elements within a
tag using an XPath expression. For example:
# Locate the <tbody> element using an XPath expression
tbody_element = driver.find_element_by_xpath("//tbody")
tr_elements = tbody_element.find_elements(By.TAG_NAME, "tr")
Q: What is the difference between find_elements_by_tag_name() and find_elements_by_xpath()?
A: find_elements_by_tag_name()
returns a list of elements that match the specified tag name, while find_elements_by_xpath()
returns a list of elements that match the specified XPath expression.
Q: How do I use Selenium's WebDriverWait
class to wait for multiple elements to be present on the page?
A: You can use the WebDriverWait
class with the until
method to wait for multiple elements to be present on the page. For example:
from selenium import webdriver
from selenium.webdriver.common.by import
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.example.com")
tbody_element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.TAG_NAME, "tbody"))
)
tr_elements = WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located((By.TAG_NAME, "tr"))
)
Q: What are some common mistakes to avoid when iterating through elements with Selenium?
A: Some common mistakes to avoid when iterating through
elements with Selenium include:
- Not waiting for the element to be present on the page
- Not using the correct method to locate the element (e.g.,
find_element_by_tag_name()
vs. find_elements_by_tag_name()
)
- Not accessing the attributes of the element correctly (e.g., using
get_attribute()
vs. text
)
- Not handling dynamic content correctly (e.g., using
WebDriverWait
to wait for the element to be present on the page)
WebDriverWait
class to wait for the element to be present on the page. For example:from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.example.com")
tbody_element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.TAG_NAME, "tbody"))
)
tr_elements = tbody_element.find_elements(By.TAG_NAME, "tr")
A: You can use the get_attribute()
method to access the attributes of an element. For example:
# Iterate through the list of <tr> elements
for tr_element in tr_elements:
# Get the text of the current <tr> element
text = tr_element.get_attribute("textContent")
print(text)
Q: What is the difference between find_element_by_tag_name() and find_element_by_xpath()?
A: find_element_by_tag_name()
returns a single element that matches the specified tag name, while find_element_by_xpath()
returns a single element that matches the specified XPath expression.
Q: How do I use XPath expressions to locate elements within a tag?
A: You can use the find_element_by_xpath()
method to locate elements within a
tag using an XPath expression. For example:
# Locate the <tbody> element using an XPath expression
tbody_element = driver.find_element_by_xpath("//tbody")
tr_elements = tbody_element.find_elements(By.TAG_NAME, "tr")
Q: What is the difference between find_elements_by_tag_name() and find_elements_by_xpath()?
A: find_elements_by_tag_name()
returns a list of elements that match the specified tag name, while find_elements_by_xpath()
returns a list of elements that match the specified XPath expression.
Q: How do I use Selenium's WebDriverWait
class to wait for multiple elements to be present on the page?
A: You can use the WebDriverWait
class with the until
method to wait for multiple elements to be present on the page. For example:
from selenium import webdriver
from selenium.webdriver.common.by import
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.example.com")
tbody_element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.TAG_NAME, "tbody"))
)
tr_elements = WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located((By.TAG_NAME, "tr"))
)
Q: What are some common mistakes to avoid when iterating through elements with Selenium?
A: Some common mistakes to avoid when iterating through
elements with Selenium include:
- Not waiting for the element to be present on the page
- Not using the correct method to locate the element (e.g.,
find_element_by_tag_name()
vs. find_elements_by_tag_name()
)
- Not accessing the attributes of the element correctly (e.g., using
get_attribute()
vs. text
)
- Not handling dynamic content correctly (e.g., using
WebDriverWait
to wait for the element to be present on the page)
find_element_by_xpath()
method to locate elements within a # Locate the <tbody> element using an XPath expression
tbody_element = driver.find_element_by_xpath("//tbody")
tr_elements = tbody_element.find_elements(By.TAG_NAME, "tr")
find_elements_by_tag_name()
returns a list of elements that match the specified tag name, while find_elements_by_xpath()
returns a list of elements that match the specified XPath expression.WebDriverWait
class to wait for multiple elements to be present on the page?WebDriverWait
class with the until
method to wait for multiple elements to be present on the page. For example:from selenium import webdriver
from selenium.webdriver.common.by import
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.example.com")
tbody_element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.TAG_NAME, "tbody"))
)
tr_elements = WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located((By.TAG_NAME, "tr"))
)
A: Some common mistakes to avoid when iterating through
elements with Selenium include:- Not waiting for the element to be present on the page
- Not using the correct method to locate the element (e.g.,
find_element_by_tag_name()
vs.find_elements_by_tag_name()
) - Not accessing the attributes of the element correctly (e.g., using
get_attribute()
vs.text
) - Not handling dynamic content correctly (e.g., using
WebDriverWait
to wait for the element to be present on the page)