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 the following example, we shall consider loading the HTML file at path https://pythonexamples.org/tmp/selenium/index-7.html . The contents of this HTML file is given below.

<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>
Copy

In the following program, we initialize a webdriver, navigate to a specific URL, 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 selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-7.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()
Copy

Output

How to get hidden paragraphs in Selenium Python?

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.

Related Tutorials

Quiz on Selenium

Q1. What is the method used to find 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. What is the method used to select a value from a drop-down list in Selenium Python?

Not answered

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

Not answered
Code copied to clipboard successfully 👍