-2

I want to submit forms in multiple websites. Usually I can't exactly know the form name or form id, but I know the input name that I want to submit.

Let's say there is a website which has couple of forms inside it. My code should check all of the forms, if one of them has a input value named "birthday" it will submit that form. If multiple forms has it, it will submit them all.

How can I achieve this?

Robert Zunr
  • 67
  • 1
  • 11

2 Answers2

1

You can basically loop over all forms and skip forms that don't contain the desired input:

for form in br.forms():
    if not form.find_control(name="birthday"):
         continue
    # fill form and submit here

More about find_control() here.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

You are gonna need to use an iterator to check all the forms in the website. In this case we will be using for. But this doesn't let us know about which form we are working on, it just lets us use it. So we are going to assign 0(the first form's ID) to a variable and add 1 to it as we change forms when a new iteration/loop starts.

currentForm = 0
for form in br.forms(): # For every form in the website
        currentForm += 1 # Add 1 to the current form so that the script knows which form we will be working next
        if not forms.find_control(name = "birthday"): # If the form isn't about birthday
                continue # Go to the next iteration / loop ignoring the statements below
        br.select_form(nr = currentForm) # Select the birthday form
        br.form["birthday"] = "Fill this with what you want" # Writing to the form
        br.submit() # Submit the working form

Note: x += y is equal to x = x + y

Community
  • 1
  • 1
Stam Kaly
  • 668
  • 1
  • 11
  • 26