0

在使用 firefox 配置文件设置在 selenium 测试中下载 pdf 文件时,我试图绕过 firefox 下载管理器窗口:

        FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference("browser.download.folderList", 2);
        profile.setPreference("browser.download.dir", "path/to/downloads");
        profile.setPreference("browser.download.useDownloadDir", true);
        profile.setPreference("browser.helperApps.neverAsk.openFile", "application/x-pdf, application/pdf, application/octet-stream, text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/x-pdf, application/pdf, application/octet-stream, text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
        profile.setPreference("browser.helperApps.alwaysAsk.force", false);
        profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
        profile.setPreference("browser.download.manager.focusWhenStarting", false);
        profile.setPreference("browser.download.manager.useWindow", false);
        profile.setPreference("browser.download.manager.showAlertOnComplete", false);
        profile.setPreference("browser.download.manager.closeWhenDone", false);
        FirefoxOptions options = new FirefoxOptions();
        options.setProfile(profile);
        WebDriver driver = new FirefoxDriver(options);
        driver.get("https://mozilla.github.io/pdf.js/web/viewer.html");
        Thread.sleep(3000);
        driver.findElement(By.id("download")).click();
        Thread.sleep(5000);

我尝试了不同的配置文件偏好变体,但似乎没有任何效果。

同样重要的是要注意,我看到该文件作为一些带有奇怪扩展名的随机字符串出现在下载文件夹中,如下所示:5daYs8iE.pdf.part. 如果我在没有单击下载窗口上的确定按钮的情况下退出测试,则文件会消失,但如果我单击它,则文件会保存并将其扩展名更改为 PDF

org.seleniumhq.selenium:selenium-firefox-driver:3.141.59

4

1 回答 1

0

您可以手动创建一个 Firefox 配置文件,而不是设置所有首选项,手动设置您需要的所有内容(PDF 自动下载等)并像这样加载现有配置文件:

package tyler;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.ProfilesIni;

public class Tyler {

    public static void main(String[] args) throws InterruptedException {
        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\\pavel.burgr\\Desktop\\webdrivers\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver(options);
        driver.manage().window().maximize();
        driver.get("https://mozilla.github.io/pdf.js/web/viewer.html");
        Thread.sleep(10000);
        driver.findElement(By.id("download")).click();
        Thread.sleep(10000);
    }

}
于 2019-10-30T10:07:33.807 回答