How to operate hidden or invisible web elements in Selenium Webdriver using Java

Sometime We need to handle invisible or hidden web elements but Selenium WebDriver will only interact with visible elements, therefore the text of an invisible element will always be returned as an empty string. In some cases We need to operate like as click, get text, send value etc hidden web elements. The follow attribute use to hide web element.

style="Display:none;"

 <div class="owl-prev" style="display: none;">prev</div>  

style="Display:block;"

 <div data-easeout="bounceOutLeft" data-easein="bounceInRight" data-stop="7800" data-start="300" data-height="26" data-width="184" data-y="155" data-x="183" class="md-object md-layer-1-0-0 bounceInRight" style="width: 9.58333%; height: 5.97701%; top: 35.6322%; left: 9.53125%; display: block;">DO YOU NEED AN</div>  

style="visibility:hidden;"

 <div style="position: absolute; width: 9999px; visibility: hidden; display: none; max-width: none;"></div>  

type="hidden"

 <input type="hidden" value="form-w1cPBpyG9r-0a26cL3dRYB5fM-V6O_18Ojgef4qOoXo" name="form_build_id">  

Demo Source Code

 import java.util.List;  
 import java.util.concurrent.TimeUnit;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.WebElement;  
 import org.openqa.selenium.firefox.FirefoxDriver;  
 public class Hiddenwebelements {  
   public static void main(String[] args) throws InterruptedException {  
     // create objects and variables instantiation     
     WebDriver driver = new FirefoxDriver();  
     // maximize the browser window      
     driver.manage().window().maximize();  
     // launch the firefox browser and open the application url   
     driver.get("http://www.yahoo.com/");  
     //Set timeout     
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
     //Get all display block invisible webelement and store in List variable name is number_of_displa_block   
     List<WebElement> number_of_displa_block = driver.findElements(By.cssSelector("div[style*='block']"));  
     //Print total size display block webelements   
     System.out.println("Display Block =: " + number_of_displa_block.size());  
     System.out.println("========================================================");  
     for (int i = 0; i < number_of_displa_block.size(); i++) {  
       // Print all display block Webelements text   
       if (!(number_of_displa_block.get(i).getText().trim().isEmpty())) {  
         System.out.println("Displa Block Element = " + i + " " + number_of_displa_block.get(i).getAttribute("style").trim() + " " + number_of_displa_block.get(i).getText().trim());  
       } else {  
         System.out.println("Displa Block Element = " + i + " " + number_of_displa_block.get(i).getAttribute("style").trim());  
       }  
     }  
     //Get all display hidden webelemnt and store in List variable name is number_of_visibility_hidden   
     List<WebElement> number_of_visibility_hidden = driver.findElements(By.cssSelector("div[style*='hidden']"));  
     //Print total size visibility hidden webelements    
     System.out.println("\n\nvisibility: hidden = " + number_of_visibility_hidden.size());  
     System.out.println("========================================================");  
     for (int i = 0; i < number_of_visibility_hidden.size(); i++) {  
       // Print all hidden Web elements text    
       if (!(number_of_visibility_hidden.get(i).getText().trim().isEmpty())) {  
         System.out.println("visibility: hidden Element = " + i + " " + number_of_visibility_hidden.get(i).getAttribute("style").trim() + " " + number_of_visibility_hidden.get(i).getText().trim());  
       } else {  
         System.out.println("visibility: hidden Element = " + i + " " + number_of_visibility_hidden.get(i).getAttribute("style").trim());  
       }  
     }  
     //Get all display none webelemnt and store in List variable name is number_of_display_none   
     List<WebElement> number_of_display_none = driver.findElements(By.cssSelector("div[style*='none']"));  
     //Print total size display none webelements    
     System.out.println("\n\ndisplay: none = " + number_of_display_none.size());  
     System.out.println("========================================================");  
     for (int i = 0; i < number_of_display_none.size(); i++) {  
       // Print all display none Webelements text   
       if (!(number_of_display_none.get(i).getText().trim().isEmpty())) {  
         System.out.println("display: none Element = " + i + " " + number_of_display_none.get(i).getAttribute("style").trim() + " " + number_of_display_none.get(i).getText().trim());  
       } else {  
         System.out.println("display: none Element = " + i + " " + number_of_display_none.get(i).getAttribute("style").trim());  
       }  
     }  
     //Get input tag and store in List variable name is number_of_hidden_input_Elements   
     List<WebElement> number_of_hidden_input_Elements = driver.findElements(By.tagName("input"));  
     //Print total size input hidden webelements   
     System.out.println("\n\nInput Hidden Element =: " + number_of_hidden_input_Elements.size());  
     System.out.println("========================================================");  
     for (int i = 0; i < number_of_hidden_input_Elements.size(); i++) {  
       // Print all hidden input webelements value   
       if (number_of_hidden_input_Elements.get(i).getAttribute("type").trim().equalsIgnoreCase("hidden")) {  
         //Check empty text   
         if (!(number_of_hidden_input_Elements.get(i).getAttribute("value").trim().isEmpty())) {  
           //Print hidden Element texts   
           System.out.println("Input Hidden Element = " + i + " " + number_of_hidden_input_Elements.get(i).getAttribute("value").trim());  
         }  
       }  
     }  
     // quit Firefox browser   
     driver.quit();  
   }  
 }  

Clicking an invisible or hidden Element
 List<WebElement> number_of_display_none = driver.findElements(By.cssSelector("div[style*='none']"));  
 JavascriptExecutor executor = JavascriptExecutor)driver;  
 executor.executeScript("arguments[0].click();", number_of_display_none.get(position).getText());  
OR
 WebElement invisibleelement= driver.findElement(ElementLocator);  
 JavascriptExecutor executor = JavascriptExecutor)driver;  
 executor.executeScript("arguments[0].click();", invisibleelement);  

10 comments:

  1. It is just the number of arguments at the end of the command. In this example, 0 is clicking "number_of_display_none.get(position).getText()".

    ReplyDelete
  2. Selenium WebDriver fits in the same role as RC did, and has incorporated the original 1.x bindings. It refers to both the language bindings and the implementations of the individual browser controlling code. This is commonly referred to as just "WebDriver" or sometimes as Selenium 2.

    Selenium Training in Velachery

    ReplyDelete
  3. Selenium automation testing mainly focus on test data design tips and Java training in chennai is the core thing needed for selenium automation.

    ReplyDelete
  4. Selenium web driver is used of the feature now now my selenium training coding is some time problem came.I want be using selenium web.My coding problem I'm run my code but selenium web driver doesn't working.How to occur selenium web driver operate search browser read this web site content is very well!!!
    Hadoop Training in Chennai | Selenium Training in Chennai |
    Java Training in Chennai

    ReplyDelete
  5. Thanks for sharing useful information. I learned something new from your bog. Its very interesting and informative. keep updating. If you are looking for any Hadoop admin related information, please visit our website Hadoop admin training in bangalore

    ReplyDelete
  6. Thankyou for this informative article. Keep writing such an informative blog.

    best pte coaching in ambala
    Best IELTS Coaching in Ambala

    ReplyDelete