簡體   English   中英

如何在Java中使用Selenium webdriver下載.docx文件?

[英]How to download .docx file using Selenium webdriver in Java?

任何人都可以讓我知道如何使用selenium(java)下載word文件? 我的下面的代碼不起作用。

FirefoxProfile prof = new FirefoxProfile();
prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/word");

當我點擊頁面中的“下載鏈接或圖標”時,會提示彈出窗口保存下載文件(見下圖),我需要點擊彈出窗口中的“ OK按鈕。

請告訴我如何使用Firefox執行此操作。

保存彈出窗口

試試這個

import java.awt.Robot;

並使用

Robot r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);

這將以編程方式按Enter鍵。

您需要使用ROBOT類來觸發ENTER Action事件。 在java中,如果要觸發任何事件,則必須使用Robot類以編程方式鍵入或觸發ENTER和ESCAPE等事件。

// Create object of Robot class
Robot object=new Robot();

// Press Enter
object.keyPress(KeyEvent.VK_ENTER);

// Release Enter
object.keyRelease(KeyEvent.VK_ENTER);

有關此信息,您可以使用此鏈接

使用以下設置使其工作:

FirefoxOptions options = new FirefoxOptions();
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", "C:\\Windows\\temp");
profile.setPreference("browser.download.useDownloadDir", true);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
options.setProfile(profile);
driver = new FirefoxDriver(options);

有關Preference設置的更多信息,請訪問: http//toolsqa.com/selenium-webdriver/how-to-download-files-using-selenium/

暫無
暫無

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

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