问题
I am using Watir with Ruby on Rails.
I need to save screenshots of couple of pages using Watir. I have managed to get the page that I want to open to show in a browser, but cannot save the screenshot yet. Here's my code:
@browser = Watir::Safari.new
folios_screenshot_path = Rails.root.join('screenshots/')
@page = Page.find(5)
cur_url = root_url + 'pages/' + @page.id.to_s
@browser.goto cur_url
@browser.div(:id => "page").wait_until_present
@browser.driver.save_screenshot(pagess_screenshot_path + '/' + @page.id.to_s + '.png')
@browser.close
In the page that I load, there's a div element with id 'page', and I am trying to make Watir wait till that element is loaded in the Watir browser. But in my main browser, I get the error Unable to load page within 10 seconds, and the screenshot doesn't get saved either. Any idea on what's wrong?
回答1:
There are several watir gems: watir (drives IE on windows), safariwatir (drives safari on mac), watir-webdriver (drives all popular browsers except safari on all popular operating systems).
You are using safariwatir gem, but you are trying to save screenshot using watir-webdriver's driver.save_screenshot
. I would suggest that you take a screen shot with Firefox.
Just install watir-webdriver gem and change
@browser = Watir::Safari.new
to
@browser = Watir::Browser.new :ff
For more information, read free version of my Watir book:
https://github.com/zeljkofilipin/watirbook/downloads
回答2:
Try following browser class, it works for me.
Browser::BROWSER.driver.save_screenshot(screenshot)
(reference)
来源:https://stackoverflow.com/questions/8369987/save-screenshot-with-watir