WebDriver切换浏览器窗口ITeye - 千亿集团

WebDriver切换浏览器窗口ITeye

2019-01-13 13:08:53 | 作者: 雨欣 | 标签: 窗口,测验,时分 | 浏览: 2002

当测验页面一起呈现2个或许以上窗口时分,并且新翻开的window没有namem,能够运用driver.switchTo().window(winHandleID)来进行切换,如下代码已经在IE8中测验通过:

 

 

//Store the current window handle
String winHandleBefore = driver.getWindowHandle();
//Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
 System.out.println("+++" + winHandle);
 driver.switchTo().window(winHandle);
// Close the original window
 driver.switchTo().window(winHandleBefore);
 driver.close();
//Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
 System.out.println("+++" + winHandle);
 driver.switchTo().window(winHandle);

 

当最终只要一个窗口剩余的时分,应该能够直接运用

driver.switchTo().window(driver.getWindowHandle)

去选中新的窗口

 

 

 public boolean switchToWindow(WebDriver driver,String windowTitle){
 boolean flag = false;
 try {
 String currentHandle = driver.getWindowHandle();
 Set String handles = driver.getWindowHandles();
 for (String s : handles) {
 if (s.equals(currentHandle))
 continue;
 else {
 driver.switchTo().window(s);
 if (driver.getTitle().contains(windowTitle)) {
 flag = true;
 System.out.println("Switch to window: "
 + windowTitle + " successfully!");
 break;
 } else
 continue;
 } catch (NoSuchWindowException e) {
 System.out.println("Window: " + windowTitle
 + " cound not found!", e.fillInStackTrace());
 flag = false;
 return flag;

  

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表千亿集团立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章