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-exceptto 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