2

Can someone guide me how I can achieve the following:

I am using selenium web driver java.

Whenever I click the preview button on the webpage, the pdf is opened in a new browser and I need to save that pdf with the name given dynamically.

So far I am able to click the preview button and a new browser is opened with the pdf. Here the browser doesn't have url.

After the pdf is open I am sending keys control+s.

Then save dialog window appears. I am stuck here about how to save pdf to the local drive.

The main browser is IE but i am trying in Firefox first

enter image description here

flooose
  • 499
  • 8
  • 25
Sush
  • 71
  • 1
  • 2
  • 11
  • Possible duplicate of [Chromedriver, Selenium - Automate downloads](https://stackoverflow.com/questions/26894071/chromedriver-selenium-automate-downloads) – Nemo May 07 '18 at 09:52

2 Answers2

2

You can try this code :-I think this is what you are looking for. let me know If this what you are expecting.

System.setProperty("webdriver.gecko.driver", "D:/geckodriver.exe");
    WebDriver driver = new FirefoxDriver();
    driver.manage().window().maximize();
    driver.get("http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_open_parameters.pdf");
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);    

    Thread.sleep(2000);
    java.awt.Robot robot = new java.awt.Robot();
    Thread.sleep(1000);
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_S);
    robot.keyRelease(KeyEvent.VK_S);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    Thread.sleep(2000);
    robot.keyPress(KeyEvent.VK_ENTER);
    Thread.sleep(2000);     
    robot.keyPress(KeyEvent.VK_TAB);   // file replace move to yes button
    Thread.sleep(2000);
    robot.keyPress(KeyEvent.VK_ENTER); // hit enter

Just first execute the code, see if it is working, and is it what you want.

Last three lines of code are written for replace existing pdf file. So you just first comment those three lines, execute the code and from next time, include last three lines of code

You need to use Robot Class to handle events. And let me know whether this is working at your end.

Rohhit
  • 722
  • 3
  • 12
  • 25
  • Can you send me a link of the application[if possible] for better understanding. And it will be better for me to help you. – Rohhit Jul 22 '17 at 05:38
  • Hi Rohit, thanks for code, it is working now and i am able to save pdf with default location and default name. can you help me in saving pdf in particular folder and give dynamic name pls – Sush Jul 24 '17 at 15:54
  • i am able to save in particular folder through firefox capabilities, now next step is to save with name dynamically – Sush Jul 24 '17 at 18:15
  • I will try the code for this, n will inform you... Happy for you, that my code is working for you. – Rohhit Jul 25 '17 at 04:55
  • thanks rohit, i am also trying, if u get solution pls let me know – Sush Jul 27 '17 at 22:13
  • @rohit, i found a way to rename after downloading file into folder. so i will download file first, then pick that file and rename. – Sush Aug 11 '17 at 18:35
0

I think you should try to download the file immediately instead of trying to manage that browse window.

You can set the attribute download of the a element, and then click on the element. See code below:

WebElement pdf = driver.findElement(By.cssSelector("a"));
String script = "arguments[0].setAttribute('download');"
((JavascriptExecutor)driver).executeScript(script, pdf);
pdf.click();
Buaban
  • 5,029
  • 1
  • 17
  • 33
  • Hi Buaban, when pdf is opened in brower, i need to save that pdf in specific folder in my local laptop. since there is no url, i thought of using Sendkeys control + s , when i do that above window popsup and i am struck there to save pdf – Sush Jul 24 '17 at 15:31