How to get hidden paragraphs in Selenium Python?

Selenium Python – Get hidden paragraphs

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

To get all the hidden paragraph elements in Selenium Python, call execute_script() method of the WebDriver object and pass the JavaScript as argument that returns all the hidden paragraphs.

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

driver.execute_script('return document.querySelectorAll("p[style*=\'display:none\'], p[hidden]")')

where

  • p[style*=\'display:none\'] selects those paragraphs which are hidden using style attribute, and
  • p[hidden] selects those paragraphs which are hidden using hidden attribute.

Examples

1. Get all the hidden paragraphs in the document

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 hidden>This is second paragraph.</p>
    <p hidden>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 hidden 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
hidden_paragraphs = driver.execute_script('return document.querySelectorAll("p[style*=\'display:none\'], p[hidden]")')

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

# Close the driver
driver.quit()

Output

Summary

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

Quiz on Selenium

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

Not answered

Q2. What is the method used to navigate back to the previous page in Selenium Python?

Not answered

Q3. What is the method used to switch to a different frame in Selenium Python?

Not answered

Q4. Which of the following is not a commonly used web element locator in Selenium Python?

Not answered

Q5. What is the method used to clear the text from an input field in Selenium Python?

Not answered