为什么将Firefox配置为在不另存为对话框的情况下下载文件后,Firefox却从URL下载名称为“ Document”而不是实际名称的PDF文件?

问题描述 投票:0回答:1
        FirefoxOptions options = new FirefoxOptions();
        FirefoxProfile profile = new FirefoxProfile();

        // Accept Untrusted Certificates
        profile.setAcceptUntrustedCertificates(true);
        profile.setAssumeUntrustedCertificateIssuer(false);

        //Directly download PDF


        profile.setPreference("browser.helperApps.neverAsk.openFile", "application/pdf");
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
        profile.setPreference("pdfjs.disabled", true); // disable the built-in PDF viewer

        // Set Firefox profile to capabilities
        options.setCapability(FirefoxDriver.PROFILE, profile);

我已经使用以上代码配置了firefox,以便当使用Selenium Webdriver单击PDF链接时,它可以直接下载PDF。当自动化脚本单击下载PDF的链接时,该脚本直接下载文件但是它失去了原始名称,但下载为'Document',请参阅附件以获取已下载文件的屏幕截图。enter image description here

selenium selenium-webdriver firefox geckodriver selenium-firefoxdriver
1个回答
0
投票

解决此问题的方法是http download file name-Web服务器需要将某些HTTP标头发送回客户端以设置文件名。您不会通过浏览器设置或Selenium来解决此问题。

解决此问题需要在服务器的响应中设置content-disposition HTTP标头。像这样的东西:

content-disposition: attachment; filename=TheFileNameYouWant.pdf
© www.soinside.com 2019 - 2024. All rights reserved.