You can get a handle in your window through driver.getWindowHandle() , and you can switch to the window with driver.switchTo().window("handle"); Window driver.switchTo().window("handle"); .
If you want to open a new window, you can click the link with target="_blank" on the website or execute JavaScript to open a new window. Then you will find another descriptor in driver.getWindowHandles() . A possible way could be:
WebDriver driver = new FirefoxDriver(); driver.get("https://www.google.com"); List<String> knownHandles = new ArrayList<String>(); knownHandles.add(driver.getWindowHandle()); ((JavascriptExecutor)driver).executeScript("window.open();"); // find the new handle. we are getting a set for (String handle : driver.getWindowHandles()) { if (!knownHandles.contains(handle)) { knownHandles.add(handle); break; } } String newHandle = knownHandles.get(knownHandles.size() -1 ); driver.switchTo().window(newHandle); driver.get("https://www.google.com");
Another way is to insert the anchor and click on it via JavaScript .
source share