analyticsPierce analyticsPierce - 13 days ago 5
Ruby Question

Ruby Mechanize gem not following meta refresh

I have a Ruby 2.2 automation script that uses Mechanize to log in to Google Payments. When I try to access the url Mechanize stops on the meta refresh. The content of the page is:

<!DOCTYPE html>
<html>
<head>
<title>Redirecting...</title>
<script type="text/javascript" language="javascript">
var url = 'https:\/\/accounts.google.com\/ServiceLogin?
service\x3dbilling\x26passive\x3d1209600\x26continue\x3dhttps:\/\/payments.google.com\/
payments\/home%23__HASH__\x26followup\x3dhttps:\/\/payments.google.com\/payments\/
home'; var fragment = ''; if (self.document.location.hash) {fragment = self.document.
location.hash.replace(/^#/,'');}url = url.replace(new RegExp("__HASH__", 'g'),
encodeURIComponent(fragment));window.location.assign(url);
</script><noscript><meta
http-equiv="refresh" content="0; url='https://accounts.google.com/ServiceLogin?
service&#61;billing&amp;passive&#61;1209600&amp;continue&#61;https://payments.google.com
/payments/home&amp;followup&#61;https://payments.google.com/payments/home'"></meta>
</noscript></head>

<body></body>
</html>


Here is the part of my script to get to the login screen:

@agent = Mechanize.new
@agent.follow_meta_refresh = true
page = @agent.get("http://payments.google.com/payments/home")
puts page.content


The page.content at the end only shows the above html, the meta refresh is not followed. Any suggestions on how I can follow that would be greatly appreciated.

Answer

Assuming the script isn't really reformatted like that:

url = page.body[/url = '(.*?)'/, 1]
page = @agent.get url
Comments