0
votes

I am using Watir Webdriver and a headless(phantomjs) browser to goto a website, login into it and click and download a file using javascript submit button.When I click on submit, I am redirected with 302 to a different address that i can see under my Network.This is url of the file to download.I am degugging using screenshots so i can see the phantomjs is working fine but after it hits on submit button, nothing happens.This whole procedure is working fine on firefox too.Using watir webdriver, how can i get that link and save it in database and redirect my phantomjs to download the file using that link?I tried reading github pull requests, official documentation and blog posts but i am unable to reach to any solution.Please provide me with suggestions or solutions. Even one word suggestion is also appreciated as it might help me to approach the problem.I have tried getting 'http request headers' but didn't succeed.I have browser.cookie.to_a and browser.headers is giving me only object like this Watir::HTMLElementCollection:0x000000024b88c0.Thank you

1
You need to use a custom Firefox profile. I recently figured out how to do this using selenium, though I haven't tried with Phantom. See herefor a code snippetmax pleaner
thanks @maxpleaner but I am looking for headless solution.Unicornz
Actually I am using headless in the project where that code comes from. If you start headless before starting the firefox driver, it will be in headless scope.max pleaner
thanks @maxpleaner , I found this code in the watir webdriver documentation and tried using it but unfortunately that code is working for firefox but not for phantomjs.I have added headers but still it is not working.Unicornz
See how-to-download-a-csv-file-using-phantomjs for info on downloading files with phantom. The recommendation seems t be to use Casper insteadmax pleaner

1 Answers

1
votes

I was not to find solution to my question using Phantomjs but I have solved the problem using watirwebdriver(0.9.1) headless and firefox(44.0). These are the settings i have used.

profile = Selenium::WebDriver::Firefox::Profile.new
profile['download.prompt_for_download'] = false
profile['browser.download.folderList'] = 2 # custom location
profile['browser.download.dir'] = download_directory
profile['browser.helperApps.neverAsk.saveToDisk'] = "application/pdf"
profile['pdfjs.disabled'] = true
profile['pdfjs.firstRun'] = false
headless = Headless.new
headless.start

browser = Watir::Browser.new(:firefox, :profile => profile)
browser.goto 'www.google.com'
browser.window.resize_to(1280, 720)
puts browser.title
puts browser.url