jjlee / mechanize

Stateful programmatic web browsing in Python, after Andy Lester's Perl module WWW::Mechanize .
http://wwwsearch.sourceforge.net/mechanize/
618 stars 121 forks source link

Selecting a single form amongst many #24

Closed danielpclark closed 14 years ago

danielpclark commented 14 years ago

I've been trying to select the 2nd form out of 20+ forms in a page. And it happens to be the only form in the page with a name 'send_form'.

I've tried

br.select_form(nr=1)

and

br.select_form(name='send_form')

and

for f in br.forms():
    if f.name != None:
        br.select_form(name=f.name)

The first results in every single form object from the page. The second returns a no form by that name error. And the third also returns every single form object on the page.

Now there are three fields I'm trying to access name=prospect_email[], name=prospect_name[], name=prospect_telephone[]. Now these input fields also have id's with the same name lacking the []. Now I've successfully input data into fields on other forms so I know how to do it. But when I try to access these I get an error saying the name of ... does not exist. I was figuring it's probably because I don't have the right form selected. I've spent hours on this and I'm racking my brain trying to figure it out. Help will be appreciated.

jjlee commented 14 years ago

Can you provide an example, please? That is, some specific HTML and Python code that demonstrates the problem?

danielpclark commented 14 years ago

I apologize. I tried to replicate it and I couldn't. And I tried the code again and it worked. So I'm a bit surprised. Here's the code that worked.

br.select_form(nr=1)

br.form['prospect_email[]'] = 'example@google.com'
br.form['prospect_name[]'] = 'Example Name'
br.form['prospect_telephone[]'] = '1234567890'

br.submit()

It was my mistake. The script actually loads through 3 other page forms before it gets to this point. There's a login, and then two redirect pages with hidden forms. I go through those quite easily. And then it's as I described. I spent most of a night trying code and ran into problems... but I try the same code today and it works.