使用Ruby捕获Steam页面的屏幕截图

时间:2017-01-05 10:09:18

标签: ruby-on-rails ruby capybara screen-scraping steam

我需要从Steam网页捕获截图,其中包含交易报价错误,但是对于此操作,我必须获得授权,我不知道向服务器发送了什么标头。 我正在尝试使用webshot gem执行此操作,并使用capybara填写我的凭据,但这不起作用并且它会捕获登录页面

 ws.start_session do
  visit 'https://store.steampowered.com/login/'
  within(:css, 'form[name="logon"]') do
    fill_in 'username', {:id => 'input_username', :with => 'test'}
    fill_in 'password', {:id => 'input_password', :with => 'password'}
  end
  click_button('Sign in', exact: true)
end.capture 'https://store.steampowered.com/account', 'example.png', width: 500, height: 500, quality: 85

1 个答案:

答案 0 :(得分:2)

您的错误可能是因为您的代码在继续请求帐户页面之前没有等待登录成功,因此对帐户页面的请求没有设置正确的Cookie并且被重定向回登录页面。您需要进行某种检查以确保登录已完成。像

这样的东西
ws.start_session do
  visit 'https://store.steampowered.com/login/'
  within(:css, 'form[name="logon"]') do
    fill_in 'input_username', with: 'test'
    fill_in 'input_password', with: 'password'
  end
  click_button('Sign in', exact: true)
  page.assert_text('You are now logged in!') # whatever text shows on the page after successfully logging in
end.capture 'https://store.steampowered.com/account', 'example.png', width: 500, height: 500, quality: 85