如何使用GeckoDriver Firefox和Selenium下载文件? [英] How to download files using GeckoDriver Firefox and Selenium?

查看:154
本文介绍了如何使用GeckoDriver Firefox和Selenium下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码下载文件,但无法正常工作

I used this code for download file , but its not working

FirefoxProfile profile = new FirefoxProfile();

profile.setPreference("browser.download.dir","D:\\WebDriverDownloads");
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;"); 
profile.setPreference( "browser.download.manager.showWhenStarting",false );
profile.setPreference( "pdfjs.disabled",true );


FirefoxDriver driver = new FirefoxDriver(profile);  //Shows error on this line

driver.get("http://toolsqa.com/automation-practice-form/");

driver.findElement(By.linkText("Test File to Download")).click();

Thread.sleep(5000);

这给了我错误

当我删除

个人资料"

'Profile'

形成此FirefoxDriver driver = new FirefoxDriver(profile); 然后代码成功运行,但下载文件"窗口没有关闭,文件也没有下载.

form this FirefoxDriver driver = new FirefoxDriver(profile); then code run successfully but download files window is not closing and file also not downloading.

我使用机器人代替它

Robot object=new Robot();
object.keyPress(KeyEvent.VK_DOWN);
object.keyRelease(KeyEvent.VK_DOWN);         
object.keyPress(KeyEvent.VK_ENTER);
object.keyRelease(KeyEvent.VK_ENTER);

及其正常工作.但是为什么我上面的代码不起作用?

and its working fine.But Why my above code is not working?

推荐答案

要下载文件,请点击链接,其文本为要下载的测试文件,您需要:

To download the file clicking on the link with text as Test File to Download you need to:

  • 创建一个新的 FirefoxProfile()并设置所需的首选项.
  • 使用 FirefoxOptions()的实例设置配置文件.
  • 您可以使用以下解决方案:

  • Create a new FirefoxProfile() and set the required preferences.
  • Use an instance of FirefoxOptions() set the profile.
  • You can use the following solution:

System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.dir", "C:\\Utility\\Downloads");
profile.setPreference("browser.download.folderList",2);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("browser.helperApps.neverAsk.openFile","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.useWindow", false);
profile.setPreference("browser.download.manager.focusWhenStarting", false);
profile.setPreference("browser.download.manager.showAlertOnComplete", false);
profile.setPreference("browser.download.manager.closeWhenDone", true);
FirefoxOptions options = new FirefoxOptions();
options.setProfile(profile);
WebDriver driver = new FirefoxDriver(options);
driver.get("http://toolsqa.com/automation-practice-form/");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Test File to Download"))).click();

这篇关于如何使用GeckoDriver Firefox和Selenium下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆