About this lesson
Make a hidden element visible in Selenium Java by changing its style with JavaScript
Learn Selenium JavaAdvancedJavaScript Executor
How it works
JavaScript can change any part of the DOM, including styles. On Text Messages, hiddenText has visibility: hidden set in its style attribute. A script that sets arguments[0].style.visibility to 'visible' shows it, and WebDriver sees the change at once: isDisplayed() turns true and getText() returns the paragraph's words.
The change lives only in this browser tab. Reloading the page brings back the original HTML, and nothing is saved on the server.
Making something visible this way is a test technique, not user behaviour. It is useful for reading content a test cannot reach otherwise, but a user would never see that element, so assertions that depend on it describe the DOM, not the user experience.
About the assignment
The starter gives you the executor and the hidden paragraph. You check its visibility, change its style with a script, check its visibility again and read the text that is now shown.
What you will do
- Print
isDisplayed()after the labelBefore JS:. - Run a script that sets the element's
style.visibilityto visible, passing the element asarguments[0]. - Print
isDisplayed()again after the labelAfter JS:. - Print the element's
getText()after the labelText:.
Common mistakes
Changing display instead of visibility
This element is hidden with
visibility. Settingdisplayto block changes nothing, because its display is alreadyblock.Reading the text before the change
getText()returns an empty string for a hidden element, so read it after the script.Expecting the change to survive navigation
Any reload or
get()restores the original page. Run the script again after navigating.
In a real test suite
Testers use DOM changes to remove a cookie banner that blocks a test, to reveal a hidden file input before sendKeys() uploads a file, or to fill hidden fields during setup. Keep such changes in setup steps and say why in a comment.
Lesson 60 clicked with JavaScript. Lesson 62 moves to screenshots, starting with the whole visible page.
Related lessons
- Get CSS Value with getCssValue()Read the
visibilityvalue that this script changes. - Get Element Text with getText()
getText(), which returns only text a user can see. - isDisplayed() - Check Element Visibility
isDisplayed(), which reflects the change straight away.
To try one of these calls on any page without a task, open the Online Selenium Java Compiler.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
public class Main {
public static void main(String[] args) throws Exception {
WebDriver driver = new ChromeDriver();
driver.get("https://testkru.com/Elements/TextMessages");
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement hiddenElement = driver.findElement(By.id("hiddenText"));
// TODO: Print isDisplayed() before modification
// TODO: Use JS to make it visible
// TODO: Print isDisplayed() after and its text
driver.quit();
}
}