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

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>

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()

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.

Related Tutorials

Privacy Policy Terms of Use

SitemapContact Us