Submit Web Form with WebDriver in Python


How to submit Web Page Form with Selenium WebDriver in Python?



You can follow this tutorial to enter input data and submit a Web page form with Selenium WebDriver.

Several methods from Selenium WebDriver classes will be used:

  • driver.find_element_by_tag_name() - Searches and returns the first HTML element that matches the given tag name within the loaded Web page.
  • element.find_element_by_name() - Searches and returns the first HTML element that matches the given name within the this element.
  • input.send_keys() - Simulates a user typing data into an INPUT element of a FORM element.
  • form.submit() - Submits the FORM element to the Website.
  • form.current_url - The current URL of this Web page.

1. Enter the following program,

# Copyright (c) 
from selenium.webdriver import Chrome
import time

driver = Chrome()

# get the first "form" element in the page
form = driver.find_element_by_tag_name("form")

# get the input element of the form.
query = form.find_element_by_name("Q")

# submit form
url = driver.current_url

print("Test Case - Submit a search:")
print("   Actual Value: "+url)
print("   Expected Value: ..Q=selenium...")
if (url.endswith("Q=selenium")):
   print("   Test Result: Passed")
   print("   Test Result: Failed")

# pause for 1 minute before closing

2. Compile and run it with the Selenium Client JAR file.


DevTools listening on ws://
Test Case - Submit a search:
   Actual Value:
   Expected Value: ..Q=selenium...
   Test Result: Passed

As you can see, we are able to enter a search criteria and submit the FORM element without any problem with Chrome WebDriver.


execute_script() to Run JavaScript in Java

Retrieve Web Form Elements with WebDriver in Python

Using Selenium WebDriver Client Python API

⇑⇑ Selenium Tutorials

2019-10-18, 1190🔥, 0💬