0

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

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Jaydev
  • 133
  • 2
  • 3
  • 10
  • You do not explain why you need to switch windows instead of just clicking the 'Cancel' button. If you provide the URL, then it might help to understand the problem that you're dealing with. – barak manos Jan 30 '14 at 11:06
  • the cancel button appears in a different window, i.e., the print preview. Any way of doing that without switching the windows? – Jaydev Jan 30 '14 at 11:29
  • Any chance to get the URL you're working on? – barak manos Jan 30 '14 at 11:51
  • it runs on my localhost. I dont think it will be of any use to you...http://192.168.1.103/CheckInn_Dine/RestHome/LoadKOTGrid/1 – Jaydev Jan 30 '14 at 12:21
  • do u think u can help?? – Jaydev Jan 30 '14 at 12:53
  • Maybe, but only if I have the URL of a web-page that has a behavior similar to your web-page's behavior. – barak manos Jan 30 '14 at 13:07
  • it can be any url that has a button to print the page. – Jaydev Jan 31 '14 at 06:27
  • i found a solution to handling print preview in chrome, using keypress using robot concept in java. Here is the code http://stackoverflow.com/questions/11537103/how-to-handle-print-dialog-in-selenium but even if the focus is on the print preview page, its not working for me... pls help... – Jaydev Feb 04 '14 at 05:29

1 Answers1

0

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.

MajiK
  • 637
  • 7
  • 10