How can I convert a relative link in Mechanize to an absolute one?
Asked Answered
K

3

10

Is there is a way to convert a Mechanize relative-link object to another one which contains the absolute URL.

Mechanize must know the absolute link, because I can call the click method on relative links too.

Kissinger answered 3/8, 2013 at 21:29 Comment(0)
E
23

You can just merge the page uri (which is always absolute) with the link uri:

page.uri.merge link.uri
Ester answered 4/8, 2013 at 1:8 Comment(0)
B
0

This is not specific to Mechanize, but an easy way would be to use the base URL in the <base> tag and add it to the relative URL to use for whatever purpose you want. This generally works.

But, then I'm not sure if you could call the click method on that since I don't know Mechanize that well.

Beaufort answered 3/8, 2013 at 21:37 Comment(0)
M
0

You can also use resolve

Example:

require 'mechanize'
agent = Mechanize.new

page = agent.get(url)

some_rel_url = '/something'

url = agent.resolve(some_rel_url)

Keep in mind that the other answers provided do not take into account all the possibilities to get the base url as described here

Basically this:

Image from that link

Mistymisunderstand answered 23/2, 2021 at 18:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.