如何使用selenium webdriver java切换到不同的窗口?

时间:2012-12-19 08:51:24

标签: webdriver

我正在尝试切换到单击债务定价模板时显示的新窗口。但我无法做到这一点,因此我无法进一步编写脚本...问题是我无法知道我应该在switchTo.window()中传递什么,因为显示了“定价审批网关”窗口以下是新窗口的HTML:

<*h1 class="pageType noSecondHeader">Pricing Approval Gateway<*/h1>

以下是代码:

LoginPage2.driver.findElement(By.linkText("TEST ORG")).click();

System.out.println("3.Select Dept pricing template button from the organization detail page.");

if(LoginPage2.driver.findElement(By.name("debt_pricing_template")).isDisplayed())

System.out.println("User should able to navigate to Dept pricing template and template display few question, user have answer these question for further navigation.");

LoginPage2.driver.findElement(By.name("debt_pricing_template")).click();

LoginPage2.driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);

LoginPage2.driver.switchTo().window("bPageTitle");

请告知需要添加的内容?

2 个答案:

答案 0 :(得分:0)

我从未使用它,因为在我的测试中我没有使用任何新窗口,但这应该有所帮助:

Set<string> handlers = driver.getWindowHandles();  
 if (driver.getWindowHandles().size()>= 1){  
   for(String handler : handlers){  
    driver.switchTo().window(handler);  
    if (driver.getElement(By.tagName("h1")).contains("Pricing")){  
      System.out.println("Get focus on Popup window");  
      break;  
    }  
   }  
 }  
 else System.out.println("No windows founded!");  

我对h1方法不太确定。因此,如果它没有帮助,请在打开新窗口之前尝试将当前窗口存储为String:

String mainWindow = driver.getWindowHandle();

然后点击链接(或者像现在这样做其他事情)打开新窗口。然后切换到新窗口:

 Set<string> handlers = driver.getWindowHandles();  
 for (String handler : handlers){
    if (!handler.equals(mainWindow)){
      driver.switchTo(handler);
      break;
    }
 }

然后切换回原始窗口只需:

driver.switchTo(mainWindow);

当然,driver变量是 WebDriver

的预期实例

答案 1 :(得分:-1)

driver.findElement(By.linkText("Go to Billing Summary")).click();

driver.findElement(By.linkText("01 Mar 2016")).click();

Thread.sleep(5000);

driver.findElement(By.linkText("AMS TAX")).click();

driver.findElement(By.linkText("00842")).click();

Set<String> instancewindow= driver.getWindowHandles();
    Iterator<String> it = instancewindow.iterator();
    String parent =it.next();
    String child = it.next();
    driver.switchTo().window(child);

driver.switchTo().frame("modalSubWindow");

    driver.findElement(By.linkText("View More Vehicle Details>>")).click();
    driver.switchTo().window(parent);