首页 文章

在Firefox中下载PDF的Selenium问题

提问于
浏览
2

我正在努力将我们的内部Java Selenium框架升级到最新版本3.14.0以及Firefox 61.0和Geckodriver 0.21.0 .

在使用Firefox自动下载PDF文件时,我遇到了问题 . 例如,在这个link有一个下载按钮,我可以让selenium点击 . 不打开pdf,而是打开内置的查看器 . 首选项 pdfjs.disabled 应该停用查看器,因此使用配置文件创建了驱动程序实例(并尝试了一些其他首选项) .

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中的一个错误 . 好吧无论如何,我找到了一种在运行时使用selenium来改变它的方法 .

但是现在已经跳过了pdf查看器,另一个popup正在中断下载 . Selenium甚至不知道这个弹出窗口 .

Applications下的首选项页面 about:preferences 我现在可以看到,对于内容类型 PDF document ,操作已更改为空,这似乎与 Always ask 相同 . 之前它是 Preview in Firefox 但它必须是 Save File 才能达到我的目标,即立即下载PDF而没有任何问题 .

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

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

我认为这是正确的,但现在还有另一个问题......因为虽然Firefox在给定目录中创建了必要的文件,但是selenium和firefox之间的通信失败了 . 浏览器窗口打开且运行正常,但Selenium无法启动会话 .

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

所以一般的问题是:
How can I force Firefox (controlled by selenium) to download the PDF file to my file system when clicked on the button?
任何可能性都是合理的

由于我的想法已经不多了,如果有人可以提供帮助或者至少确认这些东西是Firefox的问题,那将会很棒 .

2 回答

  • 0
    • 关闭firefox

    • 运行firefox配置文件管理器(Win R:firefox -p)

    • 创建一个名为selenium_profile的新的firefox配置文件

    • 在selenium_profile中运行firefox

    • 下载manualy您想要的PDF文件,并设置为始终下载此类文件类型

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

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

    只需编辑路径 .

  • 0

    pdfjs.enabledCache.state 添加为false .

    FirefoxOptions options = new FirefoxOptions();
    options.addPreference("browser.download.folderList", 2);
    options.addPreference("browser.download.dir", pathToDownload);
    options.addPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
    options.addPreference("pdfjs.enabledCache.state",false); 
    WebDriver driver = new FirefoxDriver(options);
    

    Geckodriver Selenium Auto Download PDFs

相关问题