package seleniumLearn1;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Liulanqi2 {
public boolean switchToWindow(String windowTitle,WebDriver driver) {
boolean flag =false;
try {
String currentHandle = driver.getWindowHandle();//返回当前窗口句柄
System.out.println("FAQ的handle是=========currentHandle======="+currentHandle);
Set<String> handles = driver.getWindowHandles();//返回所有窗口句柄
System.out.println("handles应该是有3个的:==="+handles.size());
for (String string : handles) {//遍历所有handles窗口
System.out.println("handle是===="+string);
if(string.equals("currentHandle")) {//如果窗口=FAQ的句柄
System.out.println("与FAQ的handle进行对比,是则输出--------------");
continue;//继续执行
}
else {//不是FAQ的句柄
System.out.println("不是FAQ的句柄=====");
driver.switchTo().window(string);//切换窗口到对应句柄如果不是FAQ的handle
if(driver.getTitle().contains(windowTitle)) {//切换窗口后,如果页面title=给的参数是首页
flag=true;//如果切换到首页的话,第一次打开URL进来的页面,则flag=true
System.out.println("Switch to window:"+windowTitle + "successfully!! 返回最初的页面OK");
break;
}else {
continue;
}
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.printf("Window:" + windowTitle + "cound not found!",e.fillInStackTrace());
}
return flag;
}
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "E:\\\\webDriver\\\\chromedriverV2.28.exe");
WebDriver driver =new ChromeDriver();
driver.get("https://dev.bignox.com/");
String windowTitle = driver.getTitle();//获取一进来首页的title
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
driver.findElement(By.linkText("Home")).click();
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
driver.findElement(By.linkText("Blog")).click();
System.out.println("windowTitle============"+windowTitle);
Liulanqi2 l=new Liulanqi2();
l.switchToWindow(windowTitle, driver);
driver.quit();
}
}