//获取新打开页面的Handle方法
public static String getLastHandle(WebDriver driver) { Set<String> totalHandles = driver.getWindowHandles(); ArrayList<String> lst = new ArrayList<String>(totalHandles); return lst.get(lst.size()-1); }
静态方法可以直接import,不需要声明类
import static Pages.HomePage.getLastHandle;
//新开窗口标识为first
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.open();");
String first = General.getLastHandle(driver);
//切换到first窗口
driver.switchTo().window(first);
//新窗口链接登录
driver.get("URL");
URL: 需要打开的网页链接