Skip to content
CodeKruPro

Change the page with JavaScript

Automation taskMediumAsked in: Infosys, TCS, Wipro

Sometimes a test must change the page (its HTML elements, called the DOM) to reach a state quickly, such as showing hidden text. Do the change with a script, then verify the result through normal Selenium calls, so the check itself still behaves like a user.

Modify the DOM with JavascriptExecutor

Modify DOM with JavaScript

JavaScript can modify the page DOM directly. This is useful for testing hidden elements or changing element properties.

On the Text Messages page, hiddenText has visibility: hidden.

Task:

  1. Navigate to https://testkru.com/Elements/TextMessages
  2. Use JavaScript to make the hidden text visible
  3. Verify it's now displayed and print its text

Expected output:

Before JS: false
After JS: true
Text: A Hidden Text