CodeGym /Courses /Python SELF EN /Filling Forms: Text Input and Button Clicks

Filling Forms: Text Input and Button Clicks

Python SELF EN
Level 36 , Lesson 0
Available

1. Introduction to Interacting with Form Elements

Forms on web pages are where the magic of interaction begins. Think of a form as a mysterious control panel that lets the user interact with the system. But instead of giving instructions to a robot vacuum, we're providing info to a website.

How to Enter Text into Input Fields

First off, let’s talk about how Selenium lets us enter text into input fields. This is done using the send_keys() method. Let's break it down step by step.

Python

from selenium import webdriver

# Set the path to the web driver, this is the path to your webdriver copy
driver = webdriver.Chrome(executable_path='path/to/chromedriver')

# Open the web page
driver.get('http://example.com/login')

# Find the input field (e.g., by id) and type in text
username_field = driver.find_element_by_id('username')
username_field.send_keys('my_super_account')

password_field = driver.find_element_by_id('password')
password_field.send_keys('my_super_secret_password')

In this simple example, we open a webpage and locate input elements by their ID. Then, we use the send_keys() method to pass text into the fields. Yep, it’s as easy as taking copy-paste to the next level!

A Handy Trick for Filling Input Fields

If the form feels a bit funky, it might be because there's already text in the input field. Before entering new text, clear the old one using the clear() method.

Python

username_field.clear()
username_field.send_keys('new_value')

This tiny trick will save you a ton of headaches, promise!

2. Clicking Buttons

Now that our input fields are filled, it’s time to click a button, like submitting the form. With Selenium, this is done using the click() method. Sort of like hitting the "Show More Memes" button, but programmatically.

Python

# Find the button by its name and click it
login_button = driver.find_element_by_name('login')
login_button.click()

And just like that, our Selenium script just became a click-master, capable of more than just clicking the "Make a Choice" button in games!

Examples of Usage

Let’s look at an example that combines send_keys() and click() to fill out and submit a registration form.

Python

# Open the registration page
driver.get('http://example.com/register')

# Fill out the registration form
driver.find_element_by_id('first_name').send_keys('Ivan')
driver.find_element_by_id('last_name').send_keys('Ivanov')
driver.find_element_by_id('email').send_keys('ivan@example.com')
driver.find_element_by_id('password').send_keys('superSecret123')

# Click the "Register" button
register_button = driver.find_element_by_css_selector('.register-button')
register_button.click()

This simple example shows how you can automate the process of registering on a site. It can be handy for testing registration forms and simulating user activity.

3. Handling Tricky Forms

Sometimes pages like to play tricks on you. You click a button, and nothing happens. Or even worse—the page throws an error. Let’s discuss some common issues and how to solve them.

Working with Dynamic Elements

Sometimes elements on the page don’t load right away. In such cases, your script might try to interact with an element before it’s loaded. It’s like starting to make an omelet before the eggs are out of the fridge.

To avoid these quirks, use waits. Selenium lets you wait until an element becomes available:

Python

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Wait until the element becomes clickable before clicking
register_button = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, '.register-button'))
)
register_button.click()

With WebDriverWait, you can tell Selenium to wait until the element is ready for action. This makes your script much more reliable.

Interacting with Invisible Elements

Some elements on the page might be invisible—like hidden buttons or fields. This could cause an error when trying to interact with them. In such a case, you can make the element visible using JavaScript:

Python

driver.execute_script("arguments[0].style.visibility='visible';", hidden_element)
hidden_element.click()

This method lets you interact with elements that are hidden for some reason.

4. Practical Application: Form Automation

Now that we’ve covered how to interact with forms, let’s consider where this can be applied in real life.

Your boss decided that everyone should fill out daily reports via a web form. And of course, you’d like to automate this boring process a bit. Using Selenium, you can create a script that automatically fills out and submits the form with your report data every day.

Example of Automating a Report

Let’s pretend we have a site with a daily report form. The form includes fields for your name, department, and a description of completed work. Let’s automate this:

Python

driver.get('http://example.com/daily_report')

# Fill out the report form
driver.find_element_by_id('name').send_keys('Anna Petrova')
driver.find_element_by_id('department').send_keys('Technologies')
driver.find_element_by_id('report').send_keys('Fixed all the bugs today!')

# Submit the form
submit_button = driver.find_element_by_id('submit')
submit_button.click()

Such a script can save you precious time and avoid human error. Keep in mind that for complex forms, it’s necessary to test each step and consider potential errors.

And that’s it for today’s lecture! Today we learned how to type into fields and click buttons like pros, turning online form systems into your automation playground.

Keep experimenting with Selenium and stick with us to learn more about turning your browser into an unstoppable automation machine!

1
Task
Python SELF EN, level 36, lesson 0
Locked
Entering text into a form and clearing it
Entering text into a form and clearing it
2
Task
Python SELF EN, level 36, lesson 0
Locked
Filling out a form and clicking a button
Filling out a form and clicking a button
3
Task
Python SELF EN, level 36, lesson 0
Locked
Handling dynamically changing forms
Handling dynamically changing forms
4
Task
Python SELF EN, level 36, lesson 0
Locked
Automating Daily Report Submission
Automating Daily Report Submission
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION