简体   繁体   English

无法使用 Selenium java 在 Chrome 浏览器中下载 pdf 文件

[英]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.我的用例:我必须从 pdf 中读取数据,然后在 chrome 浏览器中打开,并检查 pdf 中是否存在某些特定数据。

As I could not achieve the above I thought of downloading the file on my computer and use PDFbox for the verification.由于我无法实现上述,我想到了在我的计算机上下载文件并使用PDFbox进行验证。 I created a chrome profile with settings to download the pdf file directly(Settings>Content settings>PDF documents).我创建了一个 chrome 配置文件,设置为直接下载 pdf 文件(设置>内容设置>PDF文档)。 I have set it as chrome options in my selenium script.我已在我的 selenium 脚本中将其设置为 chrome 选项。 The test works but when the pdf gets opened it does not start downloading.测试有效,但当 pdf 被打开时,它不会开始下载。 The PDF file opens in my chrome browser. PDF 文件在我的 chrome 浏览器中打开。 Chrome version: 62.0.3202.94 Chrome 版本:62.0.3202.94

I got the chrome profile path from:我从以下位置获得了 chrome 配置文件路径:

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();
}

You should disable the pdf viewer plugin to inhibit the pdf file to open in chrome.您应该禁用 pdf 查看器插件以禁止在 chrome 中打开 pdf 文件。 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);

I could download pdf in Chrome without creating a new user profile.我可以在 Chrome 中下载 pdf,而无需创建新的用户配置文件。 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);
      }

Just Add in type registry只需在类型注册表中添加

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

Using Chrome options,使用 Chrome 选项,
Disable plugin - Chrome PDF Viewer ,禁用插件 - Chrome PDF 查看器
Enable plugin - always_open_pdf_externally ,启用插件 - always_open_pdf_externally
Set own Download path - download.default_directory设置自己的下载路径 - 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);

check this it is working perfectly to download the pdf检查这个它可以完美地下载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要验证 pdf 内容,请使用此

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();

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 selenium webdriver 在 chrome 中下载 pdf 文件 - How to download a pdf file in chrome using selenium webdriver 如何使用selenium java下载pdf文件? - How to download pdf files using selenium java? 如何使用Selenium和Java或任何其他工具从Chrome浏览器下载API调用 - How to download API calls from chrome browser using Selenium and java or any other tool 使用 Selenium Java 下载 PDF 在 Chrome 中不起作用 - Downloading PDF using Selenium Java not working in Chrome 是否可以使用 selenium 在 chrome 中禁用文件下载 - Is it possible to disable file download in chrome using selenium Docker & Selenium-Java:- 无法在 docker 容器上运行的 chrome 浏览器中上传图像/文件 - Docker & Selenium-Java :- Unable to upload Image/file in chrome browser running on a docker container Selenium (Java) - Chrome Headless - 无法上传文件 - Selenium (Java) - Chrome Headless - Unable to upload the file 无法使用JavaMail下载PDF附件和java - Unable to download PDF attachment with java by using JavaMail 硒下载按钮表示浏览器以Java下载的zip文件 - Selenium download button indicates a zip file download by browser in Java 无法使用 Selenium Webdriver java 在 Linux 机器上运行 Headless Chrome 浏览器 - Unable to run Headless Chrome Browser on Linux Machine using Selenium Webdriver java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM