Java中的chromedriver把Selenium的焦点转移到新窗口

driver.navigate().to("someurl.com");
    String parent = driver.getWindowHandle();
    System.out.println("Parent Window ID is : "+parent);
    driver.findElement(By.xpath("Link_of_Terms_of_Use_opens_New_Window")).click();
    System.out.println("Home Page is loaded Successfully and Terms of Use Link is clicked");
    Set<String> allWindows = driver.getWindowHandles();
    int count = allWindows.size();
    System.out.println("Total Windows : "+count);
    for(String child:allWindows)
    {
        if(!parent.equalsIgnoreCase(child))
        {
            driver.switchTo().window(child);
            System.out.println("Child Window Title is: "+driver.getTitle());
            //perform all your tasks in the new window here
            driver.close();
        }
    }
    driver.switchTo().window(parent);
    System.out.println("Parent Window Title is: "+driver.getTitle());

 

posted @ 2021-02-04 08:15  锐洋智能  阅读(351)  评论(0编辑  收藏  举报