Python Central HubSign in

Automation and Scripting / Phase 3: Web Automation & Scraping

Handling Web Forms and Buttons

Fill forms, click buttons, submit inputs, and extract results with Selenium.

Filling inputs

Python
from selenium import webdriver
from selenium.webdriver.common.by import By
 
 
driver = webdriver.Chrome()
try:
    driver.get("https://httpbin.org/forms/post")
 
    driver.find_element(By.NAME, "custname").send_keys("Alice")
    driver.find_element(By.NAME, "custtel").send_keys("123")
    driver.find_element(By.NAME, "custemail").send_keys("a@example.com")
 
    # submit
    driver.find_element(By.CSS_SELECTOR, "form").submit()
 
    print(driver.title)
finally:
    driver.quit()

Tips

  • If .submit() doesn’t work, click a submit button element instead
  • Always prefer stable selectors (IDs, name)

How Selenium interacts with a form

A form is a set of elements: text inputs, checkboxes, radio buttons, dropdowns, and buttons. Selenium finds each element and acts on it.

  • send_keys("text") types into an input. It appends to what is there, so call clear() first when a field already has a value.
  • click() clicks a button, checkbox, or radio button.
  • Select(element).select_by_visible_text("Large") chooses a dropdown option. Import it with from selenium.webdriver.support.ui import Select.
  • element.get_attribute("value") and element.text read values back.

form.submit() in the example submits the form directly, but it skips the button's click handlers and any JavaScript that runs on it. On many modern sites that means nothing happens, or the data is wrong. Clicking the real submit button is the safer default.

A fuller example

This fills the practice form at httpbin.org, chooses options, waits for the button, clicks it, and reads the result.

Python
import json
 
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
 
driver = webdriver.Chrome()
try:
    driver.get("https://httpbin.org/forms/post")
    wait = WebDriverWait(driver, 10)
 
    name = wait.until(EC.visibility_of_element_located((By.NAME, "custname")))
    name.clear()
    name.send_keys("Alice")
    driver.find_element(By.NAME, "custtel").send_keys("123")
    driver.find_element(By.NAME, "custemail").send_keys("a@example.com")
 
    driver.find_element(By.CSS_SELECTOR, "input[name='size'][value='medium']").click()
    driver.find_element(By.CSS_SELECTOR, "input[name='topping'][value='bacon']").click()
 
    submit = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "form button")))
    submit.click()
 
    body = wait.until(EC.presence_of_element_located((By.TAG_NAME, "pre"))).text
    print(json.loads(body)["form"]["custname"])
finally:
    driver.quit()

The site echoes the submitted form as JSON in a <pre> element, so the last two lines confirm the data arrived. Waiting for element_to_be_clickable avoids clicking a button that is not ready, which is a common source of ElementNotInteractableException.

Safety

A script that submits forms acts on real systems. Test against a practice site or a test account before pointing it at a real one. Do not automate forms that send messages, place orders, or change data unless you are sure of the effect. Never put passwords in the script. Read them from environment variables. Respect the site's terms, and do not use a script to get around a CAPTCHA.

Common mistakes

  • Locating by position or by visible text that changes. Use id or name first.
  • Not waiting for the next page after the click, then reading stale content.
  • Forgetting clear() on prefilled fields.
  • Sending keys to a hidden field, which raises an error.
  • Trusting submit() on JavaScript-driven forms.

Check your understanding

quizCheck yourself
  1. A text field already contains a default value. How do you replace it?

    Show answer

    B — Call clear() and then send_keys() — send_keys types at the end of the existing text. clear() empties the field first.

  2. Why is clicking the submit button usually safer than calling form.submit()?

    Show answer

    A — Clicking runs the page's own JavaScript handlers, as a user would — submit() sends the form directly and skips the click handlers and validation scripts that many sites rely on.

  3. What is the best way to locate a form field?

    Show answer

    B — By a stable id or name attribute — id and name attributes rarely change. Position and text change with each redesign or translation.

Was this page helpful?

Let us know how we did