public void swithWindowTest() { driver.findElement(By.className("open")).click(); String winHandleBefore = driver.getWindowHandle(); // 获取当前窗口的句柄 for (String winHandle : driver.getWindowHandles()) { // 遍历所有窗口的句柄 if (driver.switchTo().window(winHandle).getTitle().contains("百度")) { driver.switchTo().window(winHandle); // 切换到title中包含“百度”的窗口中 break; } } driver.findElement(By.id("kw")).sendKeys("selenium baidu"); // 输入selenium baidu driver.findElement(By.id("su")).click(); driver.switchTo().window(winHandleBefore); // 切换到之前的窗口 driver.findElement(By.id("user")).sendKeys("selenium Demo"); // 输入selenium demo }
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/191561.html