unknownbits
unknownbits

Reputation: 2885

how to download file to a specific location on .click of watir?

when I click on a link of a web page in chrome web browser with ruby watir

ie.input(:id, "c_ImageButton_Text").click

.text file start downloading. I want to download it to a specific location.I tried this code,but still getting.text file in default download location.How can I download .text file to specific location?

download_directory = "#{Dir.pwd}/downloads"
download_directory.gsub!("/", "\\") if  Selenium::WebDriver::Platform.windows?

profile = Selenium::WebDriver::Chrome::Profile.new
profile['download.prompt_for_download'] = false
profile['download.default_directory'] = download_directory

b = Watir::Browser.new :chrome, :profile => profile

click here for this code

Upvotes: 1

Views: 1930

Answers (2)

andriy-baran
andriy-baran

Reputation: 739

For latest watir version 6.6.1

  prefs = {
    prompt_for_download: false,
    default_directory: Rails.root.join('tmp').to_s
  }
  options = Selenium::WebDriver::Chrome::Options.new
  options.add_preference(:download, prefs)
  browser = Watir::Browser.new :chrome, options: options

Upvotes: 3

bbbco
bbbco

Reputation: 1530

As the old saying goes, "There is more than one way to skin a cat":

You can certainly try to skin it the way you are attempting to do in your example code. However, there are a number of issues with this course of action. Namely, it is browser specific.

I really like how Elemental Selenium suggests achieving valid tests and assertions on downloading files by using the rest-client gem to curl the file's URI and assert the expectations of the file from from there: http://elemental-selenium.com/tips/8-download-a-file-revisited

Upvotes: 0

Related Questions