Chloe
Chloe

Reputation: 26264

How do I find the path and name of a file downloaded in PHP with Selenium web driver for Chrome?

After I download a file using Selenium and Chrome driver, how do I get the name of the file?

This site avoids the question but gives examples for simple cases: http://ardesco.lazerycode.com/index.php/2012/07/how-to-download-files-with-selenium-and-why-you-shouldnt/

My link is a JavaScript link that pops up a new window and automatically downloads the file, and the file name is dynamically generated on the server.

This site suggests changing the download location, but for Firefox: http://elementalselenium.com/tips/2-download-a-file

This lists all the command line options but there are none for setting the download filter: http://www.ericdlarson.com/misc/chrome_command_line_flags.html

This question suggests that you can change the directory, but the answer is in Java and it doesn't work for PHP: Chrome Web Driver download files

I tried the following, but it gave an error:

  $options = new ChromeOptions();
  $options->setExperimentalOption('download.default_directory', '\\temp');
  $capabilities = DesiredCapabilities::chrome(); // htmlUnitJS()
  $capabilities->setCapability(ChromeOptions::CAPABILITY, $options);
  $driver = RemoteWebDriver::create($host, $capabilities, 5000);

PHP Fatal error: Uncaught exception 'UnknownServerException' with message 'unknown error: cannot parse capability: chromeOptions from unknown error: unrecognized chrome option: download.default_directory

Possibly relevant: https://groups.google.com/forum/#!topic/macenterprise/cmSKIyzjQA8 https://github.com/facebook/php-webdriver/wiki/ChromeOptions

Upvotes: 3

Views: 3058

Answers (1)

Chloe
Chloe

Reputation: 26264

I changed it to this and it worked. It did not like the \temp path, and it wanted an associative array.

  $options = new ChromeOptions();
  $prefs = array('download.default_directory' => 'c:/temp/');
  $options->setExperimentalOption('prefs', $prefs);
  $capabilities = DesiredCapabilities::chrome(); // htmlUnitJS()
  $capabilities->setCapability(ChromeOptions::CAPABILITY, $options);
  $driver = RemoteWebDriver::create($host, $capabilities, 5000);

Upvotes: 2

Related Questions