Reputation: 9
Using Ruby
and Watir
, can I save a web page the same way as doing a right mouse-click
and " save page with name
"?
I need to save the current web page from a script.
Upvotes: 0
Views: 2047
Reputation: 2901
Yes, you can do it with watir
. Just open a page and save the browser.html
to any destination you want:
b = Watir::Browser.new :phantomjs # I am using phantomjs for scripted browsing
b.goto 'http://google.com'
File.open('/tmp/google', 'w') {|f| f.write b.html }
Upvotes: 4
Reputation: 33
I don't know about watir, but I know the way to do it using Selenium Web Driver is using the page source method. Check out the docs for that here: http://selenium.googlecode.com/git/docs/api/rb/Selenium/WebDriver/Driver.html#page_source-instance_method
using this, you should get the whole source.
You can now save the source, by just creating a new file. I haven't tried this but you can check it out.
driver = Selenium::WebDriver.for(:firefox)
driver.get(URL_of_page_to_save)
file = File.new(filename, "w")
file.puts(driver.page_source)
file.close
Not sure if this saves all elements of the page.
Hope this helped a bit!
Upvotes: 0