- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a Javascript executor for making an element visible in Selenium Webdriver?
We can create a JavaScript Executor for making an element visible in Selenium webdriver. A hidden element has a style attribute whose value set to display: none.
For making an element visible on the page we have set the value of style attribute to block/ inline/ flex/ inline-block. Let us see the html code of an element which is visible(style= display: block) −
Now on clicking the Hide button, the Hide/Show Example edit box becomes invisible on the page. Let us now see the html code of the Hide/Show Example edit box in hidden state(style= display: none) −
JavaScript Executor can make the same element visible on the page. Selenium executes JavaScript commands with the help of the executeScript method.
JavaScript command to be executed is passed as a parameter to this method.
First we shall identify the element with the help of document.getElementById method and set the style.display attribute as 'block'. This shall be passed as a parameter to executeScript method.
Syntax
JavascriptExecutor j = (JavascriptExecutor)driver; j.executeScript("document.getElementById('displayed-text').style.display='block';");
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.JavascriptExecutor; public class MakeElementVisible{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); //implicit wait driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); //launch URL driver.get("https://learn.letskodeit.com/p/practice"); WebElement l = driver.findElement(By.id("displayed-text")); // Javascript Executor to make hidden element visible JavascriptExecutor j = (JavascriptExecutor) driver; j.executeScript("document.getElementById('displayed-text').style.display='block';"); //verify element is visible boolean b = l.isDisplayed(); if(b){ System.out.println("Element is visible"); }else{ System.out.println("Element is not visible"); } driver.quit(); } }
Output
- Related Articles
- Verifying whether an element present or visible in Selenium Webdriver
- How to check if an element is visible with WebDriver?
- Finding an element in a sub-element in Selenium Webdriver.
- How can we use JavaScript Executor to click and enter data to a web element in Selenium?
- How to get an attribute value of an element in Selenium Webdriver?
- How to Use WebDriver Javascript Executor to Navigate to a URL using Postman?
- How to verify an attribute is present in an element using Selenium WebDriver?
- How to set whether an element should be visible in JavaScript?
- How to click on a button with Javascript executor in Selenium with python?
- How to click on hidden element in Selenium WebDriver?
- How to click on a link with a Javascript executor in Selenium with python?
- How to verify color of a web element in Selenium Webdriver?
- How to Resolve Stale Element Reference Exception in Selenium WebDriver?
- Test if an element is focused using Selenium Webdriver.
- How to use JavaScript in Selenium to click an Element?
