代码:
package com.utils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Set;
@Component
public class SwitchWindow {
private WebDriver driver;
@Autowired
SwitchWindow(Browser browser) {
this.driver = browser.getDriver();
PageFactory.initElements(browser.getDriver(), this);
}
public void changeWindowHandle(){
String search_handle = driver.getWindowHandle(); //获取当前窗口的句柄
Set<String> handles = driver.getWindowHandles(); //获取所有窗口的句柄
for (String handle : handles) {
if (handle.equals(search_handle) == false) {
//切换到新打开页面窗口的句柄
driver.switchTo().window(handle);
System.out.println("now new open window handle!");
}
}
}
}
package com.utils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.stereotype.Component;
@Component
public class Browser {
static WebDriver driver;
static {
System.setProperty("webdriver.chrome.driver","D:\\SpringWeb\\src\\main\\resources\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
}
public WebDriver getDriver(){return driver;}
}