无法使用Selenium java

时间:2017-12-07 00:19:32

标签: java google-chrome selenium pdf

我的用例:我必须从pdf中读取数据,而不是在chrome浏览器中打开,并检查pdf中是否存在某些特定数据。

由于我无法实现上述目标,我想到了在我的计算机上下载文件并使用PDFbox进行验证。 我创建了一个chrome配置文件,其中包含直接下载pdf文件的设置(设置>内容设置> PDF文档)。 我在我的selenium脚本中将它设置为chrome选项。 测试工作但是当pdf打开时它不会开始下载。 PDF文件在我的Chrome浏览器中打开。 Chrome版本:62.0.3202.94

我从以下位置获得了chrome配置文件路径:

chrome://version/

我不确定出了什么问题。 请帮忙。

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

5 个答案:

答案 0 :(得分:1)

我可以在Chrome中下载pdf而无需创建新的用户个人资料。如果有人在寻找类似的答案,我想我可以在这里发帖:

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

答案 1 :(得分:1)

只需添加注册表类型

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

答案 2 :(得分:0)

您应禁用pdf viewer插件以禁止以chrome格式打开pdf文件。添加此chrome选项。

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

答案 3 :(得分:0)

检查这是否完美地下载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;
 }}

要验证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();

    }

}

答案 4 :(得分:0)

使用Chrome选项,
禁用插件- Chrome PDF查看器
启用插件- always_open_pdf_externally
设置自己的下载路径- download.default_directory

const passport = require('passport');

exports.login = passport.authenticate('local', {
     failureRedirect: '/login',
     successRedirect: '/'
})