In this Python Selenium article we want to learn How to Handle Forms with Python Selenium, Forms are an important part of many web applications, and handling them correctly is important when automating web browsing tasks using Python Selenium. in this article we want to talk that how to effectively handle forms with Python Selenium.
How to Handle Forms with Python Selenium
For working with the example of this tutorial you need some requirements, first you should have installed Python in your system, then we need to install Python Selenium and you can use pip for that like this, also you need to driver for specific browser.
Note: You can download the drivers from here.
First we need to import our required modules
1 2 |
from selenium import webdriver from selenium.webdriver.common.by import By |
For handling forms, we need to launch a browser instance using Selenium and open the target website.
1 2 |
driver = webdriver.Chrome() # Or webdriver.Firefox() for Firefox driver.get("https://demoqa.com/login") |
To interact with form fields, we need to locate them on the web page. Identify the necessary form fields using appropriate locator strategies, such as By.ID, By.NAME, By.XPATH or By.CSS_SELECTOR. For example, to locate the username and password fields, we can use By.ID:
1 2 |
username_field = driver.find_element(By.ID, "userName") password_field = driver.find_element(By.ID, "password") |
After that we have located the form fields, we can input values into them using the send_keys() method. For example, to enter the username and password, we already have an account in the website, and these are our credentials, you can use the mentioned website for testing purposes.
1 2 |
username_field.send_keys("geekscoders") password_field.send_keys("Geekscoders@123") |
After filling in the required form fields, we can submit the form by locating and clicking the submit button.
1 2 |
submit_button = driver.find_element(By.ID, "login") submit_button.click() |
After that you have completed the form handling and verification, remember to close the browser using the close() method.
1 |
driver.close() |
This is the complete code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
from selenium import webdriver from selenium.webdriver.common.by import By # Launch the browser driver = webdriver.Chrome() # Or webdriver.Firefox() for Firefox # Open the website driver.get("https://demoqa.com/login") # Locate the form fields username_field = driver.find_element(By.ID, "userName") password_field = driver.find_element(By.ID, "password") # Input values into the form fields username_field.send_keys("geekscoders") password_field.send_keys("Geekscoders@123") # Submit the form submit_button = driver.find_element(By.ID, "login") submit_button.click() # Navigate to the profile page driver.get("https://demoqa.com/profile") # Close the browser driver.close() |
Learn More on Python Selenium
- Web Automation with Python Selenium
- Page Navigation with Python Selenium
- Python Selenium and CSS Selectors
- Python Selenium and XPath Usage
- Working with Python Selenium Cookies
- Implicit and Explicit Waits in Python Selenium
- Handling Popups with Python Selenium
- How to Capture Screenshots with Python Selenium
- Handling Frames with Python Selenium