Skip to content
CodeKruPro

Change the Page with JavaScript

66/80

Change the Page with JavaScript

#hiddenText starts with visibility: hidden, so is_displayed() is false and .text is empty. Set the visibility to visible and read it again.

Task:

  1. Print whether #hiddenText is displayed
  2. Make it visible with JavaScript
  3. Print the new state and the text

Print the lines below exactly.

Expected output:

Before JS: False
After JS: True
Text: A Hidden Text
Read full tutorial on Codekru

About this lesson

How to make a hidden element visible in Selenium Python with a style change

Selenium Python AssignmentsAdvancedJavaScript

How it works

A p with the inline style visibility: hidden is still in the DOM, so find_element finds it, but is_displayed() returns False and text is an empty string. Selenium reports what a user would see.

Through execute_script you pass the element in as arguments[0] and set its style.visibility property. The browser re-renders at once, and the same object now reports True and returns its text, trimmed even though the markup has a leading space.

Hiding has several forms. Setting display to none also gives False and an empty text, while the textContent attribute still held the words. The element stays the same object after the change, so no second find_element is needed.

About the assignment

You check an element that starts hidden, reveal it with script, and read it again.

What you will do

  1. Find the hidden paragraph and print its displayed state, converting the bool with str().
  2. Run a script on the element that sets its visibility style to the value that shows it.
  3. Print the displayed state again, then print the text on a third line.
  4. Take the first reading before the change, or the contrast is lost.

Common mistakes

  • Forgetting str() on the boolean

    Adding the bool to a label stops the script with a TypeError.

  • Passing a string where the element belongs

    A string argument fails with JavascriptException: Cannot set properties of undefined (setting 'visibility').

  • Expecting text from a hidden element

    Reading .text before the change returns an empty string without an exception. Blank means hidden, not wordless.

In a real test suite

Forcing an element visible is a diagnostic tool. A test that does it to get past a real hidden control proves nothing about what users can do, so teams mostly use it in throwaway scripts.

Next comes a failure you meet daily: an element that stops working after a reload.

To try one of these calls on any page without a task, open the Online Selenium Python Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

main.py
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
try:
    driver.get("https://testkru.com/Elements/TextMessages")
    # Make #hiddenText visible and print the three lines
finally:
    driver.quit()
Read the full tutorial