簡體   English   中英

使用Selenium WebDriver for Firefox下載pdf

[英]Downloading pdf with Selenium WebDriver for Firefox

我正在嘗試將.pdf下載到我的本地,以便我可以使用Apache PDFBox從中讀取文本並將其驗證為我的測試套件的一部分。 我已經找到了一些代碼,可以通過點擊URL從Firefox下載pdf。 這對我不起作用,因為我正在使用的pdf是一個機密文檔,因此它不會被URL公開,而是作為彈出窗口加載到PDF Viewer中。 在瀏覽器中加載PDF Viewer后,有誰知道如何點擊Firefox PDF Viewer中的下載按鈕?

在此輸入圖像描述

我試過通過元素的id =“download”來查找它:

(new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("download")));
driver.findElement(By.id("download")).click(); 

不幸的是,這不起作用,因為它說它找不到元素。 有人知道解決方法嗎?

更新:我描述的彈出窗口是一個iframe元素。 這導致無法找到“下載”元素。 修復了@ 4M01的switchTo()答案。

如你所說,

而是在PDF Viewer中作為彈出窗口加載

您需要使用驅動程序對象的switchTo()方法處理不同窗口之間的切換。

下面的代碼工作正常,沒有問題,我可以點擊下載圖標。

public class FirefoxPDFTest {
      WebDriver driver;

    @BeforeClass
    void Setup(){
        System.setProperty("webdriver.gecko.driver", "C:\\Automation\\Selenium\\drivers\\geckodriver.exe");
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
    }

    @Test
    void downloadPDF(){
        driver.get("http://www.pdf995.com/samples/pdf.pdf");
        waitTillPageLoad();
        driver.findElement(By.id("download")).click();
    }



    private void waitTillPageLoad(){
        new WebDriverWait(driver, 30).until(driver -> ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete"));
    }


    @AfterClass
    void tearDown(){
        driver.close();
        driver.quit();
    }

}

只需使用以下代碼點擊下載按鈕:

    driver.findElement(By.xpath("//button[@id='download']")).click();

    Thread.sleep(8000);

    Robot robot = new Robot();

    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);

我們可以使用Firefox瀏覽器設置和使用WebDriver的Firefox Profile設置在Firefox瀏覽器中處理下載彈出窗口。

步驟1:在Firefox瀏覽器中更新設置。

打開Firefox瀏覽器並導航到工具 - >選項導航到應用程序。 將Action類型設置為PDF的“保存文件”。

第2步:使用FirefoxProfile初始化FireFoxDriver

File downloadsDir = new File("");

// Set Preferences for FirefoxProfile.
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", downloadsDir.getAbsolutePath());
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
      "application/msword, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream");
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("browser.download.manager.focusWhenStarting", false);
profile.setPreference("browser.download.useDownloadDir", true);
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
profile.setPreference("browser.download.manager.closeWhenDone", true);
profile.setPreference("browser.download.manager.showAlertOnComplete", false);
profile.setPreference("browser.download.manager.useWindow", false);
profile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
profile.setPreference("pdfjs.disabled", true);

// Initialize the FireFoxDriver instance.
FirefoxDriver webDriver = new FirefoxDriver(profile);

第3步:執行腳本

執行單擊下載PDF圖標的腳本。

結果:將下載PDF文件,並且不會顯示“下載”彈出窗口。

您可以使用以下(C#)代碼處理下載圖標(使用Firefox):

IWebElement element = Driver.FindElement(By.Id("download"));
IJavaScriptExecutor executor = (IJavaScriptExecutor)Driver;
executor.ExecuteScript("arguments[0].click();", element);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM