Ritica Ramakrishnan
Ritica Ramakrishnan

Reputation: 79

Unable to download pdf file in Chrome Browser using Selenium java

My use case: I have to read data from a pdf than opens in chrome browser and check if some specific data is present in the pdf or not.

As I could not achieve the above I thought of downloading the file on my computer and use PDFbox for the verification. I created a chrome profile with settings to download the pdf file directly(Settings>Content settings>PDF documents). I have set it as chrome options in my selenium script. The test works but when the pdf gets opened it does not start downloading. The PDF file opens in my chrome browser. Chrome version: 62.0.3202.94

I got the chrome profile path from:

chrome://version/

I am not sure what went wrong. Please help.

    @Before
      public void beforeTest() throws MalformedURLException{

          System.setProperty("webdriver.chrome.driver","path to chromedriver\\chromedriver.exe"); 
          ChromeOptions options = new ChromeOptions();
          String chromeProfilePath="path to custom chrome profile";
          options.addArguments("user-data-dir="+chromeProfilePath);
          HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
          DesiredCapabilities cap = DesiredCapabilities.chrome();
          cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
          cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
          cap.setCapability(ChromeOptions.CAPABILITY, options);
          driver = new ChromeDriver(cap);
          //Browser is maximized
          driver.manage().window().maximize();
}

Upvotes: 1

Views: 10036

Answers (5)

Dilshan Harshana
Dilshan Harshana

Reputation: 137

Using Chrome options,
Disable plugin - Chrome PDF Viewer,
Enable plugin - always_open_pdf_externally,
Set own Download path - download.default_directory

ChromeOptions options = new ChromeOptions();
HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
chromeOptionsMap.put("plugins.plugins_disabled", new String[] {
        "Chrome PDF Viewer"
});
chromeOptionsMap.put("plugins.always_open_pdf_externally", true);
options.setExperimentalOption("prefs", chromeOptionsMap);

String downloadFilepath = "D:\\Lime Doc";
chromeOptionsMap.put("download.default_directory", downloadFilepath);

ChromeDriver driver = new ChromeDriver(options);

Upvotes: 1

arun swain
arun swain

Reputation: 11

Just Add in type registry

[HKEY_LOCAL_MACHINE\Software\Policies\Google\Chrome] "AlwaysOpenPdfExternally"=dword:00000001

Upvotes: 1

Ritica Ramakrishnan
Ritica Ramakrishnan

Reputation: 79

I could download pdf in Chrome without creating a new user profile. I thought I could post it here if anyone is looking for a similar answer:

@Before
      public void beforeTest() throws Exception{

                  System.setProperty("webdriver.chrome.driver","path to chromedriver.exe");
          ChromeOptions options = new ChromeOptions();
          HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
          chromeOptionsMap.put("plugins.plugins_disabled", new String[] {
                    "Chrome PDF Viewer"
                });
          chromeOptionsMap.put("plugins.always_open_pdf_externally", true);
          options.setExperimentalOption("prefs", chromeOptionsMap);
          String downloadFilepath = "download folder path";
          chromeOptionsMap.put("download.default_directory", downloadFilepath);
          DesiredCapabilities cap = DesiredCapabilities.chrome();
          cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
          cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
          cap.setCapability(ChromeOptions.CAPABILITY, options);
          driver = new ChromeDriver(cap);
          //Browser is maximized
          driver.manage().window().maximize();
          //Browser navigates to the url
          driver.navigate().to("URL");
          driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      }

Upvotes: 1

heardm
heardm

Reputation: 37

check this it is working perfectly to download the pdf

package testing;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class pdfdownload {
 static String urls ="http://www.staff.amu.edu.pl/~zcht/pliki/Databases%20for%20beginners.pdf";

 public static void main(String[] args) throws IOException {
  URL url = verify(urls);

  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  InputStream inputStream = null;
  String filename = url.getFile();
  filename = filename.substring(filename.lastIndexOf('/')+1);
  FileOutputStream outputStream = new FileOutputStream("D:\\HELLO/java" + File.separator+ filename);

  inputStream = connection.getInputStream();

  int read = -1;
  byte[] buffer = new byte[4096]; 

  while((read = inputStream.read(buffer))!= -1){
   outputStream.write(buffer,0,read);

  }
  inputStream.close();
  outputStream.close();
 }

 private static URL verify(String url){ 
  if(!url.toLowerCase().startsWith("http://")){
   return null;
  }
  URL verifyURL= null;

  try{
   verifyURL = new URL(url);

  }catch(Exception e){

  }
  return verifyURL;
 }}

To verify pdf content use this

package pdf;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;
import org.testng.Assert;


public class pdfreader {

    public static void main(String[] args) throws IOException {

        File file = new File("D://study video tutorials//database testing//Database Testing Quick Guide.pdf");
        FileInputStream fis = new FileInputStream(file);

        PDFParser parser = new PDFParser(fis);
        parser.parse();

        COSDocument cosDoc= parser.getDocument();       
        PDDocument pddoc= new PDDocument(cosDoc);
        PDFTextStripper strip= new PDFTextStripper();
        String data = strip.getText(pddoc);
        System.out.println(data);

        Assert.assertTrue(data.contains("keys"));
        cosDoc.close();
        pddoc.close();

    }

}

Upvotes: 0

Manmohan_singh
Manmohan_singh

Reputation: 1804

You should disable the pdf viewer plugin to inhibit the pdf file to open in chrome. Add this chrome option.

ChromeOptions options = new ChromeOptions();
Map<String, Object> preferences = new Hashtable<String, Object>();
options.setExperimentalOption("prefs", preferences);

// disable flash and the PDF viewer
preferences.put("plugins.plugins_disabled", new String[] {
    "Chrome PDF Viewer"
});

// launch the browser and navigate to the page
ChromeDriver driver = new ChromeDriver(options);

Upvotes: 1

Related Questions