Mechanize - follow or "click" meta updates in rails

I have some problems with Mechanize.

When submitting a form using Mechanize. I came to a page with one meta update and there are no links.

My question is: how can I keep track of meta updates?

I tried to enable meta refresh, but then I got a socket error. Code example

require 'mechanize'
agent = WWW::Mechanize.new
agent.get("http://euroads.dk")
form = agent.page.forms.first
form.username = "username"
form.password = "password"
form.submit
page = agent.get("http://www.euroads.dk/system/index.php?showpage=login")
agent.page.body

Answer:

<html>
 <head>
   <META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=index.php?showpage=m_frontpage\">
 </head>
</html>

Then I try:

redirect_url = page.parser.at('META[HTTP-EQUIV=\"Refresh\"]')[
  "0;URL=index.php?showpage=m_frontpage\"][/url=(.+)/, 1]

But I get:

NoMethodError: Undefined method '[]' for nil: NilClass
+5
source share
2 answers

Mechanize Nokogiri HTML DOM. Nokogiri, XPath, CSS-, .

URL- Nokogiri:

require 'nokogiri'

html = <<EOT
<html>
  <head>
    <meta http-equiv="refresh" content="2;url=http://www.example.com/">
    </meta>
  </head>
  <body>
    foo
  </body>
</html>
EOT

doc = Nokogiri::HTML(html)
redirect_url = doc.at('meta[http-equiv="refresh"]')['content'][/url=(.+)/, 1]
redirect_url # => "http://www.example.com/"

doc.at('meta[http-equiv="refresh"]')['content'][/url=(.+)/, 1] : (at) CSS <meta> http-equiv refresh. content , url=.

Mechanize . , :

agent = Mechanize.new
page = agent.get('http://www.examples.com/')
redirect_url = page.parser.at('meta[http-equiv="refresh"]')['content'][/url=(.+)/, 1]
page = agent.get(redirect_url)

EDIT: at('META[HTTP-EQUIV=\"Refresh\"]')

at(). , . , , , , , . Nokogiri , <meta http-equiv=\"Refresh\"...>.

EDIT: Mechanize , :

 agent.follow_meta_refresh = true

. :

parse (content, uri)

URL- . Parse , uri URL-, URL- . , url . nil, URL .

# <meta http-equiv="refresh" content="5;url=http://example.com/" />
uri = URI.parse('http://current.com/')

Meta.parse("5;url=http://example.com/", uri)  # => ['5', 'http://example.com/']
Meta.parse("5;url=", uri)                     # => ['5', 'http://current.com/']
Meta.parse("5", uri)                          # => ['5', 'http://current.com/']
Meta.parse("invalid content", uri)            # => nil
+4

. , , :

page = agent.get("http://www.euroads.dk/system/index.php?showpage=login")
page.meta_refresh.first.click
+2

All Articles