Alex Stahl
2010-Jul-08 22:50 UTC
[Mechanize-users] How to access value of URL in a Page object?
Hiya mechanize-users - I''m using mechanize to click a log out button on a website, which returns a Mechanize::Page object upon completion. When either ''puts''ing or ''inspect''ing this page object, I can see that the first hash (? it looks like a hash) contained within the object is the URL of the response page. There is no accessor for the URL, and trying to access as a member var fails. How can I programmatically get to that value? (Some assumptions - I''m already logged in when I call .get, and let''s assume here that I always get the "logged out" page assigned to res). Sample code: req = Mechanize.new base = "http://www.example.com" res = req.click(req.get(base).link_with(:text => /log out/)) p res Included in the output: {url #<URI::HTTP:0xb7507d34 URL:http://www.example.com/login>} How do I get that url value? These don''t work: res.url res(:url) And can I do so w/out having to resort to a search or an iterator? Thanks, Alex
Alex Stahl
2010-Jul-08 23:35 UTC
[Mechanize-users] How to access value of URL in a Page object?
res.uri On Thu, 2010-07-08 at 17:50 -0500, Alex Stahl wrote:> Hiya mechanize-users - > > I''m using mechanize to click a log out button on a website, which > returns a Mechanize::Page object upon completion. When either ''puts''ing > or ''inspect''ing this page object, I can see that the first hash (? it > looks like a hash) contained within the object is the URL of the > response page. There is no accessor for the URL, and trying to access > as a member var fails. How can I programmatically get to that value? > > (Some assumptions - I''m already logged in when I call .get, and let''s > assume here that I always get the "logged out" page assigned to res). > > Sample code: > > req = Mechanize.new > base = "http://www.example.com" > res = req.click(req.get(base).link_with(:text => /log out/)) > p res > > Included in the output: > {url #<URI::HTTP:0xb7507d34 URL:http://www.example.com/login>} > > How do I get that url value? These don''t work: > res.url > res(:url) > > And can I do so w/out having to resort to a search or an iterator? > > Thanks, > Alex > > _______________________________________________ > Mechanize-users mailing list > Mechanize-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/mechanize-users
Takashi Ogura
2010-Jul-09 06:36 UTC
[Mechanize-users] How to access value of URL in a Page object?
Hi If you want to get url value as a character string, you can use the to_s method. req = Mechanize.new base = "http://www.example.com" res = req.click(req.get(base).link_with(:text => /log out/)) p res.uri.to_s #=> "http://www.example.com/login" regards, Takashi Ogura 2010/7/9 Alex Stahl <astahl at hi5.com>:> res.uri > > > On Thu, 2010-07-08 at 17:50 -0500, Alex Stahl wrote: >> Hiya mechanize-users - >> >> I''m using mechanize to click a log out button on a website, which >> returns a Mechanize::Page object upon completion. ?When either ''puts''ing >> or ''inspect''ing this page object, I can see that the first hash (? it >> looks like a hash) contained within the object is the URL of the >> response page. ?There is no accessor for the URL, and trying to access >> as a member var fails. ?How can I programmatically get to that value? >> >> (Some assumptions - I''m already logged in when I call .get, and let''s >> assume here that I always get the "logged out" page assigned to res). >> >> Sample code: >> >> req = Mechanize.new >> base = "http://www.example.com" >> res = req.click(req.get(base).link_with(:text => /log out/)) >> p res >> >> Included in the output: >> {url #<URI::HTTP:0xb7507d34 URL:http://www.example.com/login>} >> >> How do I get that url value? ?These don''t work: >> res.url >> res(:url) >> >> And can I do so w/out having to resort to a search or an iterator? >> >> Thanks, >> Alex >> >> _______________________________________________ >> Mechanize-users mailing list >> Mechanize-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/mechanize-users > > > _______________________________________________ > Mechanize-users mailing list > Mechanize-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/mechanize-users >