Filling out online forms manually is time-consuming and repetitive. Python can automate this process using libraries like Selenium and PyAutoGUI to interact with web elements and input data efficiently.
Why Automate Web Form Submissions?
Saves time on repetitive tasks
Reduces human errors
Efficient for bulk form submissions
Can be scheduled for automatic execution
1. Setting Up the Environment
Install the required libraries:
pip install selenium pandas openpyxl pyautogui
- Selenium → Interacts with web forms
- Pandas & OpenPyXL → Reads data from Excel
- PyAutoGUI → Simulates keyboard & mouse actions
2. Automating a Simple Web Form Submission
Steps to Automate:
- Open a web browser
- Navigate to the form
- Locate input fields (name, email, etc.)
- Fill in details and submit
Python Code Using Selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
# Start the browser
driver = webdriver.Chrome()
# Open the form webpage
driver.get("https://example.com/form")
# Locate fields and enter data
driver.find_element(By.NAME, "name").send_keys("John Doe")
driver.find_element(By.NAME, "email").send_keys("john.doe@example.com")
driver.find_element(By.NAME, "phone").send_keys("1234567890")
# Submit the form
driver.find_element(By.NAME, "submit").click()
# Wait and close
time.sleep(3)
driver.quit()
print("Form submitted successfully!")
Now the form is filled and submitted automatically!
3. Automating Bulk Form Submissions from Excel
We can read data from an Excel file and submit multiple entries.
Sample Excel Data (data.xlsx
):
Name | Phone | |
---|---|---|
Alice | alice@example.com | 9876543210 |
Bob | bob@example.com | 8765432109 |
Python Code to Read Data & Submit Forms
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
# Load Excel Data
data = pd.read_excel("data.xlsx")
# Open Browser
driver = webdriver.Chrome()
driver.get("https://example.com/form")
# Loop through each row in Excel
for _, row in data.iterrows():
driver.find_element(By.NAME, "name").send_keys(row["Name"])
driver.find_element(By.NAME, "email").send_keys(row["Email"])
driver.find_element(By.NAME, "phone").send_keys(str(row["Phone"]))
# Submit Form
driver.find_element(By.NAME, "submit").click()
time.sleep(2) # Wait before next entry
# Close browser
driver.quit()
print("Bulk form submissions completed!")
Now multiple forms are submitted from Excel!
4. Automating Web Form Submission with Keyboard Actions
For websites that block Selenium, we can use PyAutoGUI to simulate user input.
Simulating Form Input Using PyAutoGUI
import pyautogui
import time
time.sleep(3) # Give time to focus on the input field
# Type name
pyautogui.write("John Doe", interval=0.1)
pyautogui.press("tab")
# Type email
pyautogui.write("john.doe@example.com", interval=0.1)
pyautogui.press("tab")
# Type phone number
pyautogui.write("1234567890", interval=0.1)
pyautogui.press("enter")
print("Form submitted with PyAutoGUI!")
Now forms are filled even if automation is restricted!
5. Scheduling Form Submissions
Use Python’s schedule
module to run the script automatically.
Example: Submit Form Daily at 9 AM
import schedule
import time
def submit_form():
print("Running form submission script...")
# Call the Selenium script here
schedule.every().day.at("09:00").do(submit_form)
while True:
schedule.run_pending()
time.sleep(60)
Now the script runs at 9 AM daily!
6. Handling CAPTCHAs in Web Forms
CAPTCHAs prevent bots from submitting forms. Some solutions:
Manual intervention → Pause the script and enter CAPTCHA
Using third-party CAPTCHA-solving services (e.g., Anti-CAPTCHA, 2Captcha)
If using Google reCAPTCHA v2, solve using selenium-click()
, if supported