Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill and submit html form

I am trying / wanting to write a Python script (2.7) that goes to a form on a website (with the name "form1") and fills in the first input-field in said form with the word hello, the second input-field with the word Ronald, and the third field with [email protected]

Can anyone help me code or give me any tips or pointers on how to do this ?

like image 621
IrfanM Avatar asked Oct 06 '12 03:10

IrfanM


People also ask

How do I fill an HTML form automatically?

The autocomplete attribute specifies whether a form should have autocomplete on or off. When autocomplete is on, the browser automatically complete values based on values that the user has entered before. Tip: It is possible to have autocomplete "on" for the form, and "off" for specific input fields, or vice versa.

How do you submit a form in HTML?

The Submit Button The <input type="submit"> defines a button for submitting the form data to a form-handler. The form-handler is typically a file on the server with a script for processing input data. The form-handler is specified in the form's action attribute.

How do I add a submit button to a form?

It is also created using HTML <input> tag but type attribute is set to button. You can try to run the following code to use submit button to submit and reset a form. Under the action, attribute add the file, where you want to reach after clicking Submit button.


1 Answers

Aside from Mechanize and Selenium David has mentioned, it can also be achieved with Requests and BeautifulSoup.

To be more clear, use Requests to send request to and retrieve responses from server, and use BeautifulSoup to parse the response html to know what parameters to send to the server.

Here is an example script I wrote that uses both Requests and BeautifulSoup to submit username and password to login to wikipedia:

import requests
from bs4 import BeautifulSoup as bs


def get_login_token(raw_resp):
    soup = bs(raw_resp.text, 'lxml')
    token = [n['value'] for n in soup.find_all('input')
             if n['name'] == 'wpLoginToken']
    return token[0]

payload = {
    'wpName': 'my_username',
    'wpPassword': 'my_password',
    'wpLoginAttempt': 'Log in',
    #'wpLoginToken': '',
    }

with requests.session() as s:
    resp = s.get('http://en.wikipedia.org/w/index.php?title=Special:UserLogin')
    payload['wpLoginToken'] = get_login_token(resp)

    response_post = s.post('http://en.wikipedia.org/w/index.php?title=Special:UserLogin&action=submitlogin&type=login',
                           data=payload)
    response = s.get('http://en.wikipedia.org/wiki/Special:Watchlist')

Update:

For your specific case, here is the working code:

import requests
from bs4 import BeautifulSoup as bs


def get_session_id(raw_resp):
    soup = bs(raw_resp.text, 'lxml')
    token = soup.find_all('input', {'name':'survey_session_id'})[0]['value']
    return token

payload = {
    'f213054909': 'o213118718',  # 21st checkbox
    'f213054910': 'Ronald',  # first input-field
    'f213054911': '[email protected]',
    }

url = r'https://app.e2ma.net/app2/survey/39047/213008231/f2e46b57c8/?v=a'

with requests.session() as s:
    resp = s.get(url)
    payload['survey_session_id'] = get_session_id(resp)
    response_post = s.post(url, data=payload)
    print response_post.text
like image 121
K Z Avatar answered Oct 07 '22 22:10

K Z