How to Handle Textboxes and Buttons in Selenium (Python)
How to Handle Textboxes and Buttons in Selenium (Python)
Selenium is a powerful tool used for automating web applications. One of the most common tasks in Selenium is interacting with textboxes and buttons—like filling out forms or clicking submit.
In this post, you’ll learn how to:
-
Enter text in a textbox
-
Click a button
-
Clear a textbox
-
Check if buttons/textboxes are enabled or visible
✅ Getting Started
First, install Selenium (if not already):
Also, download the appropriate WebDriver (like ChromeDriver) and place it in your project folder or system path.
๐ ️ Basic Setup
๐ฅ How to Handle Textboxes
๐ธ 1. Find Textbox Element
Use find_element()
and a locator like ID, name, or XPath.
๐ธ 2. Enter Text
๐ธ 3. Clear Text
๐ธ 4. Check If Textbox Is Enabled
๐ฑ️ How to Handle Buttons
๐ธ 1. Find the Button
You can also use:
-
By.NAME
-
By.XPATH
-
By.CSS_SELECTOR
๐ธ 2. Click the Button
๐ธ 3. Check If Button Is Visible and Enabled
๐ก Example: Simple Login Automation
⚠️ Best Practices
-
Always use unique locators (ID preferred)
-
Add waits if elements take time to load
-
Use
try-except
to handle missing elements
๐ Summary
Task | Selenium Code |
---|---|
Enter text | element.send_keys("text") |
Clear textbox | element.clear() |
Click button | element.click() |
Check visibility | element.is_displayed() |
Check enabled | element.is_enabled() |
๐ง Final Thoughts
Handling textboxes and buttons is the foundation of web automation in Selenium.
With just a few lines of code, you can fill forms, log in to websites, or test UI workflows.
Comments
Post a Comment