In the url http://letskodeit.teachable.com/pages/practice
, there is a button element called Open Window.
I can inspect the element button called "openwindow". I tried using "id" and "xpath" for this element. but the error says "can't find the element". I have implicit wait. I can inspect the element button called "openwindow". I tried using "id" and "xpath" for this element. but the error says "can't find the element". I have implicit wait.
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class WindowHandles {
private WebDriver achromeDriver;
private String baseUrl;
@Before
public void setUp() throws Exception {
baseUrl = "http://letskodeit.teachable.com/pages/practice";
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\ChromeDirver\\chromedriver.exe");
achromeDriver = new ChromeDriver();
achromeDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
achromeDriver.manage().window().maximize();
System.out.println("setup completed");
}
@Test
public void test() throws InterruptedException { // Get the handle
String parentHandle = achromeDriver.getWindowHandle();
System.out.println("Parent Handle: " + parentHandle);
// Find Open Window button
System.out.println("before finding the element");
WebElement openWindow = achromeDriver.findElement(By.xpath("//button[@name='openwindow']"));
openWindow.click();
// Get all handles
Set<String> handles = achromeDriver.getWindowHandles();
// Switching between handles
for (String handle: handles) {
System.out.println(handle);
}
// Switch back to the parent window
}
@After
public void tearDown() throws Exception {
}
}