使用下面的代碼:
System.setProperty("webdriver.edge.driver","D:\\Workspace\\StackOverlow\\src\\lib\\MicrosoftWebDriver.exe"); //put actual location
WebDriver driver = new EdgeDriver();
driver.navigate().to("https://www.google.com");
driver.manage().window().maximize();
Robot robot;
try {
robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_P);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.keyRelease(KeyEvent.VK_P);
Thread.sleep(3000);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String parentWindowHandler = driver.getWindowHandle();
String subWindowHandler = null;
Set<String> handles = driver.getWindowHandles();
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
subWindowHandler = iterator.next();
driver.switchTo().window(subWindowHandler);
System.out.println(subWindowHandler);
}
driver.get("https://stackoverflow.com/");
//driver.switchTo().window(parentWindowHandler); // Uncomment this line if you want to use normal browser back
}
注意,我們使用機器人類,因此,如果系統鎖定它可能無法正常工作。
如果我的答案有助於解決您的問題,請點擊我的答案左側的正確標記接受答案,正下方的上下選舉票。請投票了..它將真正幫助我和其他人誰將在未來提及這個問題..這是如何stackoverflow工作:) ...參考: - https://meta.stackexchange.com/questions/5234/如何-不接受-的回答工作 –