如何使用Selenium和GeckoDriverFirefox直接下载多个文件

问题描述

我有一个Web服务,当调用它时,它会提供csv文件(MIME application/x-excel)和以下Kotlin代码(仅相关片段):

System.setProperty(
        "webdriver.gecko.driver","D:\\Programme\\geckodriver-v0.27.0-win64\\geckodriver.exe")

val profile = FirefoxProfile()
profile.setPreference("browser.download.folderList",2)
profile.setPreference("browser.download.manager.showWhenStarting",false)
profile.setPreference("browser.download.manager.closeWhenDone",true)
profile.setPreference("browser.download.dir",outputDir.absolutePath)
profile.setPreference(
        "browser.helperApps.neverAsk.saveToDisk","application/x-excel")

val options = FirefoxOptions()
options.profile = profile

driver = FirefoxDriver(options)

urlsForMonth.forEachIndexed { index,uri ->
    log.info("$uri")
    driver.get(uri)       // first get works fine,but never returns
    log.info("Done")      // never comes to this line
}

第一个文件下载正常,但随后发生超时。第二条日志行未触发。我已经知道driver.get()已用于导航到URL并等到页面加载完毕(触发了onLoad)。因此,这解释了行为。但是driver.navigate().to()不应该等待页面加载,也不要从调用返回。

当我在服务器主页上跳出循环然后执行navigate().to()进行下载时,我能够下载文件1(发生完成的日志行),然后挂在第二个文件上navigate().to()通话。

任何人都可以给我提示我缺少什么吗?

org.openqa.selenium.TimeoutException: Timeout loading page after 300000ms
Build info: version: '3.141.59',revision: 'e82be7d358',time: '2018-11-14T08:17:03'
System info: host: 'bla',ip: '10.35.60.213',os.name: 'Windows 10',os.arch: 'amd64',os.version: '10.0',java.version: '11.0.2'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true,browserName: firefox,browserVersion: 68.6.1,javascriptEnabled: true,moz:accessibilityChecks: false,moz:buildID: 20200403065309,moz:geckodriverVersion: 0.27.0,moz:headless: false,moz:processID: 21068,moz:profile: C:\Users\myself\AppData\Lo...,moz:shutdownTimeout: 60000,moz:useNonSpecCompliantPointerOrigin: false,moz:webdriverClick: true,pageLoadStrategy: normal,platform: WINDOWS,platformName: WINDOWS,platformVersion: 10.0,rotatable: false,setWindowRect: true,strictFileInteractability: false,timeouts: {implicit: 0,pageLoad: 300000,script: 30000},unhandledPromptBehavior: dismiss and notify}
Session ID: d1d6ec79-13e9-4fa0-9560-da0112966fe4

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...