Selenium is an automation tool for web browsers that is extensively used to interact with input fields on web pages. Such activities include filling out forms, testing user input functionality, and setting values in text boxes.
This article is about discovering how to assign values to input elements by making use of Selenium WebDriver in a variety of different use cases.
Table of Contents
1. Introduction to Selenium and Web Elements
What is Selenium?
It is an open-source web browser automation framework widely used for testing web applications, web scraping, and other automation processes.
Understanding Web Elements
The basic building blocks of a web page are called web elements, such as input fields, buttons, and links. Through these, Selenium simulates the actions that a real user would do.
To set a value in an input field, you first need to find the web element. Selenium provides several methods to find elements:
We begin by creating an object of Driver:
from selenium import webdriver
Using “find_element_by_id”
element = driver.find_element_by_id("user_name")
This method locates elements using their id attribute.
Using “find_element_by_name”
element = driver.find_element_by_name("email")
This method finds elements using their name attribute.
Using “find_element_by_xpath”
element = driver.find_element_by_xpath("//input[@type='text']")
This method locates elements using their XPath.
“Using send_keys() Method”
The send_keys() method is used to execute typing into an input field.
Example:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://intellipaat.com")
input_element = driver.find_element_by_id("intellipaat")
input_element.send_keys("IntellipaatStudentr")
To avoid appending new text to existing content, use the clear() method before send_keys().
Example:
input_element.clear()
input_element.send_keys("NewValue")
4. Handling Special Scenarios
If the input field is disabled, you can use JavaScript to set the value.
Example:
driver.execute_script("document.getElementById(intellipaat).value = intellipaatUser;")
Some input fields are controlled by JavaScript and may require triggering events after setting the value.
Example:
input_element = driver.find_element_by_id("username")
driver.execute_script("arguments[0].value = 'TestUser'; arguments[0].dispatchEvent(new Event('change'));", input_element)
Here’s a complete example:
from selenium import webdriver
from selenium.webdriver.common.by import By
# Initialize WebDriver
driver = webdriver.Chrome()
# Open the target website
driver.get("https://intellipaat.com")
# Locate the input element by ID
input_element = driver.find_element(By.ID, "intellipaat")
# Clear the input field (if necessary) and set the value
input_element.clear()
input_element.send_keys("intellipaat_user")
# Close the browser
driver.quit()
6. Conclusion
Selenium provides a very simple way to interact with input fields using the send_keys() method. Advanced scenarios, such as disabled fields or JavaScript-controlled inputs, can be addressed by executing JavaScript to set values. With these techniques mastered, you will be able to automate complex web interactions effectively.
7. FAQs
What is the difference between clear() and send_keys()?
clear() removes any existing content from the input field, while send_keys() types the specified value into the field.
Can I set a value to a disabled input field using Selenium?
Yes, you can use JavaScript to set a value to a disabled input field.
How do I simulate pressing keys like Enter or Tab?
You can use the Keys class from selenium.webdriver.common.keys. Example:
from selenium.webdriver.common.keys import Keys
input_element.send_keys(Keys.ENTER)
How can I handle dynamic input fields that change IDs?
Use XPath or CSS selectors to locate elements based on other attributes or relative positions.
Is send_keys() the only way to set input values?
No, you can also use JavaScript for more control, especially for complex input scenarios.