Handling redirects in mechanize

983 views
Skip to first unread message

Vikram Nagesh

unread,
Dec 14, 2011, 4:59:01 AM12/14/11
to Chennaipy
Hi,

I have been using Mechanize for my basic web page/web app testing. I
am stuck at a place where my page redirects as soon as I login with
the credentials, but i am not able to move to the redirected page.

My code goes some thing like this:


from mechanize import Browser

br= Browser()

br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(True)
br.set_handle_refresh(True, max_time=1)

br.open(url)

br.select_form(name="login")
br["username"] = "Credential"
br["password"] = "Credential"

resp = br.submit()

print br.title()


Output:
Example Login Redirect


In this case the output for the above code is the title of the
redirect page and not the title of the page after the redirection. My
question might be silly. As i am exploring python for testing a web
page/web app any suggestions is very much appreciated,

Thanks,
Vikram

Anand Chitipothu

unread,
Dec 14, 2011, 5:21:58 AM12/14/11
to chen...@googlegroups.com
2011/12/14 Vikram Nagesh <charm...@gmail.com>:

What do you mean by "title of the redirect page"? Are you redirecting
using javascript?

Anand

Vikram Nagesh

unread,
Dec 14, 2011, 5:50:23 AM12/14/11
to Chennaipy
I guess, yes they are using javascript for the redirect.

On Dec 14, 3:21 pm, Anand Chitipothu <anandol...@gmail.com> wrote:
> 2011/12/14 Vikram Nagesh <charming...@gmail.com>:

Anand Chitipothu

unread,
Dec 14, 2011, 5:54:15 AM12/14/11
to chen...@googlegroups.com
2011/12/14 Vikram Nagesh <charm...@gmail.com>:

> I guess, yes they are using javascript for the redirect.

Mechanize can't handle javascript redirects as it can't execute
javascript. It can only handle http redirects.

Anand

Vikram Nagesh

unread,
Dec 14, 2011, 6:05:42 AM12/14/11
to Chennaipy
So what will be the best approach to test this web page/web app,
(which uses javascript, ajax for custom controls and was done in ruby)
using python?

On Dec 14, 3:54 pm, Anand Chitipothu <anandol...@gmail.com> wrote:
> 2011/12/14 Vikram Nagesh <charming...@gmail.com>:
>

Noufal Ibrahim

unread,
Dec 14, 2011, 6:06:17 AM12/14/11
to chen...@googlegroups.com
Vikram Nagesh <charm...@gmail.com> writes:

> So what will be the best approach to test this web page/web app,
> (which uses javascript, ajax for custom controls and was done in ruby)
> using python?

Something browser based like Selenim perhaps?


[...]


--
~noufal
http://nibrahim.net.in

Christ was born in 4 B.C.

Noufal Ibrahim

unread,
Dec 14, 2011, 6:08:43 AM12/14/11
to chen...@googlegroups.com
Noufal Ibrahim <nou...@gmail.com> writes:

> Vikram Nagesh <charm...@gmail.com> writes:
>
>> So what will be the best approach to test this web page/web app,
>> (which uses javascript, ajax for custom controls and was done in ruby)
>> using python?
>
> Something browser based like Selenim perhaps?

[...]

I meant Selenium - http://seleniumhq.org/


--
~noufal
http://nibrahim.net.in

Goes (Went) over like a lead balloon.

Anand Chitipothu

unread,
Dec 14, 2011, 6:25:25 AM12/14/11
to chen...@googlegroups.com
2011/12/14 Noufal Ibrahim <nou...@gmail.com>:

> Noufal Ibrahim <nou...@gmail.com> writes:
>
>> Vikram Nagesh <charm...@gmail.com> writes:
>>
>>> So what will be the best approach to test this web page/web app,
>>> (which uses javascript, ajax for custom controls and was done in ruby)
>>> using python?
>>
>> Something browser based like Selenim perhaps?
>
> [...]
>
> I meant Selenium - http://seleniumhq.org/

Selenium is kind of heavy-weight.

If you are comfortable with javascript, you can try
http://www.phantomjs.org/. It is headless browser based on WebKit with
Javascript API. I haven't used it myself, but looks very promising.

Anand

Reply all
Reply to author
Forward
0 new messages