Selecting a single form amongst many with mechanize

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • burntbit
    New Member
    • Jun 2010
    • 3

    Selecting a single form amongst many with mechanize

    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
    Code:
    br.select_form(nr=1)
    and
    Code:
    br.select_form(name='send_form')
    and
    Code:
    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_e mail[], name=prospect_n ame[], name=prospect_t elephone[]. 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.
  • burntbit
    New Member
    • Jun 2010
    • 3

    #2
    Originally posted by burntbit
    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
    Code:
    br.select_form(nr=1)
    and
    Code:
    br.select_form(name='send_form')
    and
    Code:
    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_e mail[], name=prospect_n ame[], name=prospect_t elephone[]. 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.
    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.

    Code:
    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.

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Originally posted by burntbit
      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.

      Code:
      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.
      Thanks for the update burntbit.

      Comment

      Working...