Skip to content

Instantly share code, notes, and snippets.

@SarahElson
Created June 12, 2024 07:34
Show Gist options
  • Save SarahElson/80c2b3a49db84d579297ca61fd0006cd to your computer and use it in GitHub Desktop.
Save SarahElson/80c2b3a49db84d579297ca61fd0006cd to your computer and use it in GitHub Desktop.
How to Handle Hidden Elements in Selenium WebDriver
package com.lambdatest.automation;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.ITestContext;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
public class HiddenElementsException {
WebDriver driver=new ChromeDriver();
@Test
public void basicTest() throws InterruptedException {
// navigating to the application under test
driver.get("https://sripriyakulkarni.com/");
// maximize window
driver.manage().window().maximize();
// explicit wait - to wait for the link to be click-able
WebDriverWait wait =new WebDriverWait(driver, Duration.ofSeconds(30));
wait.until(ExpectedConditions
.visibilityOfElementLocated(By.xpath("//span[normalize-space()='Automation Practice']"))).click();
// navigating to section of hidden element
driver.findElement(By.xpath("//span[normalize-space()='Automation Practice']")).click();
Thread.sleep(1000);
// Clicking on the Hide button
driver.findElement(By.xpath("//input[@id='hide-textbox']")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Thread.sleep(1000);
driver.findElement(By.xpath("/html/body/main/section[7]/div[2]/div[2]/div/div/div/div[1]/div[2]/fieldset/input[3]")).click();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment