Firefox中的硒问题PDF下载 [英] Selenium problems with PDF download in Firefox

查看:73
本文介绍了Firefox中的硒问题PDF下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将内部Java Selenium框架与Firefox 61.0和Geckodriver 0.21.0结合在一起升级到最新版本3.14.0.

I'm working on an upgrade of our internal Java Selenium framework to the latest version 3.14.0 in combination with Firefox 61.0 and Geckodriver 0.21.0.

在使用Firefox自动下载PDF文件时,我遇到了问题.例如,在此链接处,我可以让硒执行单击的下载按钮.无需下载pdf,而是将打开内置查看器.偏好 pdfjs.disabled 应该停用查看器,因此使用配置文件创建了驱动程序实例(并尝试了更多偏好).

I face problems when it comes to an automated download of a PDF file with Firefox. For example at this link there is a download button where I can let selenium perform a click on. Instead of downloading the pdf, a built-in viewer will be opened. The preference pdfjs.disabled should deactivate the viewer so created the driver instance with a profile (and also tried some more preferences).

FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setAcceptUntrustedCertificates(true);
firefoxProfile.setAssumeUntrustedCertificateIssuer(true);
firefoxProfile.setPreference("browser.download.folderList", 2);
firefoxProfile.setPreference("network.cookie.cookieBehavior", 0);
firefoxProfile.setPreference("network.cookie.alwaysAcceptSessionCookies", true);
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
firefoxProfile.setPreference("browser.helperApps.neverAsk.openFile", "application/pdf");
firefoxProfile.setPreference("browser.helperApps.alwaysAsk.force", false);
firefoxProfile.setPreference("browser.download.manager.showAlertOnComplete", false);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
firefoxProfile.setPreference("browser.download.panel.shown", false);
firefoxProfile.setPreference("browser.download.manager.focusWhenStarting", false);
firefoxProfile.setPreference("browser.download.manager.closeWhenDone", false);
firefoxProfile.setPreference("browser.download.manager.useWindow", false);
firefoxProfile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
firefoxProfile.setPreference("browser.download.manager.alertOnEXEOpen", false);
firefoxProfile.setPreference("pdfjs.disabled", true);

FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setProfile(firefoxProfile);
WebDriver webdriver = new FirefoxDriver(firefoxOptions);

不幸的是,对于使用的Firefox版本,此配置无效.除非您在运行时在 about:config 页面中将 pdfjs.disabled 更改为 true .只有在运行时第一次更改pdf查看器时,它才真正被禁用.似乎是Firefox中的错误.好的,无论如何,我找到了一种在运行时使用硒进行更改的方法.

Unfortunately with the used Firefox version this config has no effect. Unless you change pdfjs.disabled to true at runtime within the about:config page. Only when first changed at runtime, the pdf viewer is really disabled. Seems to be a bug in Firefox. Ok anyway, I found a way to change this at runtime with selenium.

但是现在pdf查看器被跳过了,另一个弹出窗口中断了下载.硒甚至不知道此弹出窗口.

But now that the pdf viewer is skipped another popup is interrupting the download. Selenium is not even aware of this popup.

在首选项页面应用程序 about:preferences >现在我可以看到,对于内容类型 PDF文档,该操作已更改为空,这似乎与总是询问相同.在进行 Firefox预览之前,它必须是 Save File ,才能实现我的目标,即立即下载PDF而没有任何问题.

On the preferences page about:preferences under Applications I can see now that for the Content Type PDF document the Action was changed to nothing, which seems to be the same as Always ask. Before it was Preview in Firefox but it has to be Save File in order to reach my goal which is to immediately download the PDF without any questions.

如果没有单个配置,则可以将其更改为保存文件".我错了吗?但是,当我检查配置文件文件夹中的差异时,我发现有一个 handlers.json 包含上图的设置.在浏览器启动期间将其读入,并在浏览器关闭时首先进行调整.
通常selenium为每个新的浏览器实例创建一个新的临时配置文件文件夹.如果要影响所需的设置,则需要定义一个自定义配置文件.那是我的想法.所以我尝试了这个:

Not with a single config you could possibly change this to "Save File". Am I wrong? But when I checked profile folders for differences I found out that there is a handlers.json that contains the settings from the picture above. It is read in during the browser startup and first adapted on browser close.
Normally selenium creates a new temporary profile folder for each new browser instance. If I want to have an influence on the desired setting, I need to define a custom profile. That was my thought. So I tried this:

firefoxOptions.addArguments("-profile", "/tmp/my.profile");

我认为这是正确的,但是现在又出现了一个问题……因为尽管Firefox在给定目录中创建了必要的文件,但是硒与Firefox之间的通信失败.浏览器窗口已打开且运行正常,但是Selenium无法启动会话.

I think this is correct, but now yet another problem... becauses though Firefox creates the neccessary files in the given directory, the the communication between selenium and firefox fails. The browser window is opened and healthy, but Selenium is not able to start the session.

org.openqa.selenium.WebDriverException: connection refused 

            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
            at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$new$0(W3CHandshakeResponse.java:57)
            at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$getResponseFunction$2(W3CHandshakeResponse.java:104)
            at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$0(ProtocolHandshake.java:122)
            at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
            at java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)
            at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
            at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:498)
            at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485)
            at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
            at java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:152)
            at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
            at java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:464)
            at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:125)
            at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:73)
            at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:136)
            at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
            at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)
            at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:212)
            at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:130)
            at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:140)
            …

所以通常的问题是:
单击按钮时,如何强制Firefox(受硒控制)将PDF文件下载到我的文件系统中?
任何可能性合理.

So the general question is:
How can I force Firefox (controlled by selenium) to download the PDF file to my file system when clicked on the button?
Any possibilities are reasonable.

由于我的想法不多了,如果有人可以帮助或至少确认这些是Firefox的问题,那将是很好的.

As I am running out of ideas it would be great if somebody could help or at least confirm that these things are problems with Firefox.

推荐答案

  1. 关闭Firefox
  2. 运行firefox配置文件管理器(Win + R:firefox -p)
  3. 创建一个新的名为selenium_profile的Firefox配置文件
  4. 在selenium_profile中运行Firefox
  5. 手动下载所需的PDF文件,并将其设置为始终下载此类文件类型

要使用selenium_profile运行webdriver,请使用以下命令:

To run webdriver with selenium_profile, use this:

public static void setUpClass() {
    FirefoxOptions options = new FirefoxOptions();
    ProfilesIni allProfiles = new ProfilesIni();         
    FirefoxProfile selenium_profile = allProfiles.getProfile("selenium_profile");
    options.setProfile(selenium_profile);
    options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
    System.setProperty("webdriver.gecko.driver", "C:\\Users\\pburgr\\Desktop\\geckodriver-v0.20.0-win64\\geckodriver.exe");
    driver = new FirefoxDriver(options);
    driver.manage().window().maximize();}

只需编辑路径.

这篇关于Firefox中的硒问题PDF下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆