当前位置: 首页 > 知识库问答 >
问题:

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

马和硕
2023-03-14

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

由于我无法做到以上,我想到下载文件在我的电脑上,并使用PDFbox进行验证。我创建了一个chrome配置文件,带有直接下载pdf文件的设置(设置>内容设置>pdf文档)。我已经在我的selenium脚本中将其设置为chrome选项。测试工作,但当pdf打开时,它不会开始下载。PDF文件在我的chrome浏览器中打开。Chrome版本:62.0.3202.94

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

共有1个答案

宇文和同
2023-03-14

您应该禁用pdf查看器插件,以禁止pdf文件在Chrome中打开。添加此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);
 类似资料:
  • 我正在使用java selenium来自动化我的应用程序。我正在下载PDF和解析。在我的应用程序中有多个使用PDF的测试用例。因此,当我下载第一个PDF时,chrome会在浏览器底部显示一个弹出,该文件是用文件名下载的,所以下次当我试图下载第二个PDF时,下载按钮就在这个弹出下面,不能单击下载选项。使用isDisplay方法检查状态,返回false和isEnabled返回true。尝试在另一个窗口

  • 请有人帮我解决这个问题。提前道谢。当运行java代码在模拟器中打开chrome浏览器时,我收到“无法启动Chromedriver会话:无法创建新会话。详细信息:未创建会话:此版本的Chromedriver仅支持chrome 83版”错误消息。

  • 我需要在chrome浏览器上模拟文件下载,下面的链接指向我正在寻找的解决方案。 http://ardesco.lazerycode.com/index.php/2012/07/how-to-download-files-with-selenium-and-why-you-shouldnt/ https://github.com/Ardesco/Ebselen/blob/master/ebselen

  • 我正在使用以下配置: 运行时发现以下错误: org.openqa.selenium.会话未创建异常断开:无法连接到渲染器 (会话信息:chrome=62.0.3202.62) (驱动程序信息:chromeDrive=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bcfba87af1),平台=Windows NT 6.1.7601 SP1x86_64)(警告:服

  • 线程“main”java.lang.IllegalStateException中的异常:驱动程序可执行文件的路径必须由WebDriver.Chrome.driver系统属性设置;有关更多信息,请参见https://github.com/seleniumhq/selenium/wiki/chromedriver。最新版本可从http://chromedriver.storage.googleapis

  • 在我的php文件中,我有以下内容来创建一个带有FPDF库的PDF: 但是请求是响应这个,而不是打开一个保存对话框来保存我的PDF。 %PDF-1.3 3 0 obj<>endobj 4 0 obj<>stream x 3 R@2π35 W(çR qπw 3 t04多30 pispéz*[(hx·ääää+çó)·(j*dé7 w endstream endobj 1 0 obj /xobject<