python requests fill form

Python Requests: Fill Form

If you want to fill out a form on a website using Python, the requests module can be very useful. Here is how you can do it:

Step 1: Inspect the Form

The first thing you need to do is inspect the HTML code for the form on the website you want to fill out. You can do this by right-clicking on the form and selecting “Inspect” (in Chrome).

Once you have the HTML code for the form, you need to look for the <form> tag, which will contain the attributes for the form. These attributes will include the “action” attribute, which tells the browser where to send the form data when it is submitted, and the “method” attribute, which tells the browser whether to use GET or POST to submit the data.

Step 2: Create a Dictionary of Form Data

Once you know the attributes of the form, you can create a Python dictionary that contains the data you want to submit. The keys of the dictionary should be the names of the form fields, and the values should be the data you want to submit.


form_data = {
    "name": "John Doe",
    "email": "[email protected]",
    "message": "Hello, world!"
}

You can also use Python variables to store the data, like this:


name = "John Doe"
email = "[email protected]"
message = "Hello, world!"

form_data = {
    "name": name,
    "email": email,
    "message": message
}

Step 3: Send a POST Request with the Form Data

Now that you have the form data, you can use the requests module to send a POST request to the URL specified in the form’s “action” attribute. Here is how you can do it:


import requests

url = "https://example.com/contact"
form_data = {
    "name": "John Doe",
    "email": "[email protected]",
    "message": "Hello, world!"
}

response = requests.post(url, data=form_data)

print(response.status_code)
print(response.text)

The above code sends a POST request to the URL “https://example.com/contact” with the form data. The response from the server is stored in the “response” variable. You can then print the status code and the response text to see if the request was successful.

Step 4: Handle Redirects

Sometimes when you submit a form, the server will redirect you to a different page. To handle this, you can use the allow_redirects parameter of the requests.post() method:


import requests

url = "https://example.com/contact"
form_data = {
    "name": "John Doe",
    "email": "[email protected]",
    "message": "Hello, world!"
}

response = requests.post(url, data=form_data, allow_redirects=False)

if response.status_code == 302:
    print("Form submitted successfully!")
else:
    print("Form submission failed.")

The above code sets the allow_redirects parameter to False, which means that if the server sends a redirect response, the requests module will not automatically follow the redirect. Instead, you can check the status code of the response to see if the form was submitted successfully.

Alternative: Use Selenium WebDriver

If the website you want to fill out the form on uses JavaScript to dynamically update the page, you may need to use a tool like Selenium WebDriver to interact with the form. Here is how you can do it:


from selenium import webdriver

url = "https://example.com/contact"
driver = webdriver.Chrome()
driver.get(url)

name_input = driver.find_element_by_name("name")
name_input.send_keys("John Doe")

email_input = driver.find_element_by_name("email")
email_input.send_keys("[email protected]")

message_input = driver.find_element_by_name("message")
message_input.send_keys("Hello, world!")

submit_button = driver.find_element_by_xpath("//button[@type='submit']")
submit_button.click()

driver.quit()

The above code uses the Selenium WebDriver to navigate to the contact page, fill out the form fields, click the submit button, and then close the browser window. You will need to have the Selenium WebDriver installed and a compatible browser (like Chrome or Firefox) installed as well.