How to get all paragraphs using Selenium Python?

Selenium Python – Get all paragraphs

In this tutorial, you will learn how to get all the paragraph elements using Selenium in Python.

To get all the paragraph elements in Selenium Python, call find_elements() method of the element object and pass the arguments: "By.TAGNAME" for by parameter and "p" for the value parameter.

The find_elements() method returns a list of WebElement objects, where each object in the list is a paragraph element.

The following code snippet returns all the paragraphs in the page.

driver.find_elements(By.TAG_NAME, "p")

Examples

1. Get all the “p” tag elements in the page

In this example, we shall consider loading the following HTML file via the driver object.

index.html

<html>
  <body>
    <h2>Hello User!</h2>
    <p>This is first paragraph.</p>
    <p>This is second paragraph.</p>
    <p>This is third paragraph.</p>
    <h2>Another section</h2>
    <p>This is fourth paragraph.</p>
  </body>
</html>

In the following program, we initialize a webdriver, navigate to a specific URL (index.html) that is running on a local server, get all the paragraph elements, and print them to standard output using a Python For loop statement.

Python Program

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
service = ChromeService(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)

# Navigate to the url
driver.get('http://127.0.0.1:5500/index.html')

# Get all the paragraph elements
elements = driver.find_elements(By.TAG_NAME, "p")

# Iterate over the paragraph elements
for index, para in enumerate(elements):
    print(f'Paragraph {index+1} :\n{para.get_attribute("outerHTML")}\n')

# Close the driver
driver.quit()

Output

Selenium Python Example - Get all paragraphs in the document

If you want just the paragraph text, use the following code.

Python Program

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
service = ChromeService(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)

# Navigate to the url
driver.get('http://127.0.0.1:5500/index.html')

# Get all the paragraph elements
elements = driver.find_elements(By.TAG_NAME, "p")

# Iterate over the paragraph elements
for index, para in enumerate(elements):
    print(f'Paragraph {index+1} :\n{para.text}\n')

# Close the driver
driver.quit()

Output

Selenium Python Example - Get all paragraphs in the document and print their text

Summary

In this Python Selenium tutorial, we have given instructions on how to get all the paragraph elements in the document using find_elements() method of WebElement class, with example programs.

⫷ Previous tutorialNext tutorial ⫸

Quiz on Selenium

Q1. What is the method used to find web elements in Selenium Python?

Not answered

Q2. Which of the following is not a method to wait for a web element to load 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 switch to a different frame 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