简体   繁体   中英

How to switch between windows in Chrome using Selenium WebDriver with Java?

In my web appilcation, i need to click on the cancel button of the print preview window of chrome that comes up. I found some code in web to switch between the windows, but for some reason, it doesnt seem to be working. please help.

    driver.findElement(By.xpath("//*[@id='PrintBill']")).click();
    driver.findElement(By.xpath("//*[@id='savebill']")).click(); 
    // This is in the parent window.

    for(String winHandle : driver.getWindowHandles()){
        driver.switchTo().window(winHandle);
    }

    driver.findElement(By.xpath("//*[@id='print-header']/div/button[2]")).click();
    //this is in the child window(print preview window)

This is where i found the code from... http://seleniumwebdriverfaq.blogspot.in/2012/02/how-can-i-switch-webdriver-control-to_4426.html

It looks like the problem is that Chrome print page is not just another browser window but a modal dialog box which means Selenium probably can not interact with it. So what you can do instead is to use Robots. Here is an example solution for your issue. Go straight down to post #11:

Handling the print dialog box

I quickly checked it on a sample page and it works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM