14
11

More than 5 years have passed since last update.

Selenium でファイルのダウンロード

Last updated at Posted at 2014-08-26

自動的に指定したフォルダにファイルをダウンロードするように設定する。

PhantomJS ではできそうにないので Firefox Driver で。

// プロファイルを設定してドライバを作る
private def createDriver(outputDirPath: String): FirefoxDriver = {
  val profile = new FirefoxProfile()
  profile.setPreference("browser.download.folderList", 2)
  profile.setPreference("browser.download.dir", outputDirPath)
  profile.setPreference("browser.download.useDownloadDir", true)
  profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream")
  new FirefoxDriver(profile)
}

// ダウンロード待ち(waitSecondsごとにダウンロードが完了したかチェック)
private def waitDownloading(waitSeconds: Int, outputDirPath: String, downloadFileName: String): Unit = {
  val downloadFilePath = Paths.get(outputDirPath, downloadFileName)
  while (!downloadFilePath.toFile.exists || Files.size(downloadFilePath) == 0) {
    (1 to waitSeconds).foreach { _ =>
      Thread.sleep(1 * 1000)
      print(".")
    }
    println()
  }
}
14
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
11