5 Best Ways to Input Letters in Capital Letters in an Edit Box in Selenium with Python

💡 Problem Formulation: In Selenium automation testing, you might encounter a scenario where you need to enter text into a text field in uppercase. For example, you need to automate the filling of a form where the ‘Name’ field should be populated with capital letters. This article details the methods to achieve this, ensuring that when you input “john doe”, the edit box receives “JOHN DOE”.

Method 1: Using the send_keys() Function with Python’s .upper()

This method employs Python’s built-in string method .upper() to convert the input string to uppercase before it is sent to the edit box using Selenium’s send_keys() function.

Here’s an example:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('http://example.com')
edit_box = driver.find_element_by_id('inputBox')
edit_box.send_keys("your text".upper())

The output in the edit box will be “YOUR TEXT”.

This code initializes the Selenium WebDriver, navigates to a specified webpage, and locates an edit box by its ID. Then, it converts the string “your text” to uppercase using Python’s .upper() method and inputs it into the edit box using Selenium’s send_keys() function.

Method 2: Using the ActionsChains Class

The ActionsChains class in Selenium can be used to perform a sequence of actions. In this method, we use it to send the keys in uppercase directly.

Here’s an example:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()
driver.get("http://example.com")
action = ActionChains(driver)
edit_box = driver.find_element_by_id("inputBox")
action.click(edit_box).send_keys("your text".upper()).perform()

The output in the edit box will be “YOUR TEXT”.

After navigating to the website, we create an instance of the ActionChains class. This example performs a click action on the edit box and then sends the uppercase string before calling perform() to execute the chain of actions.

Method 3: Using a JavaScript Executor

With this method, Selenium WebDriver’s execute_script function is used to run JavaScript that sets the value of the edit box directly in uppercase.

Here’s an example:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('http://example.com')
script = "document.getElementById('inputBox').value = arguments[0].toUpperCase();"
driver.execute_script(script, "your text")

The output in the edit box will be “YOUR TEXT”.

This code injects JavaScript into the webpage to find the edit box by ID and set its value to the uppercase version of the provided string. The execute_script() method executes the script with “your text” as an argument.

Method 4: Using Python’s Keyboard Module

Python’s keyboard module can simulate keyboard events. This method can be used alongside Selenium to type out the capitalized text as if a user was entering it manually.

Here’s an example:

import keyboard
from selenium import webdriver

driver = webdriver.Chrome()
driver.get('http://example.com')
edit_box = driver.find_element_by_id('inputBox')
edit_box.click()  # Focus on the edit box
keyboard.write("your text".upper())

The output in the edit box will be “YOUR TEXT”.

The code snippet uses the keyboard module to mimic a user typing the text in uppercase into a focused edit box. The edit box is clicked to gain focus before the text is written.

Bonus One-Liner Method 5: Using send_keys() with String Literal

This method demonstrates a quick one-liner that combines the string conversion to uppercase and the send_keys() function in one neat Python line.

Here’s an example:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('http://example.com')
edit_box = driver.find_element_by_id('inputBox')
edit_box.send_keys("your text".join().upper())

The output in the edit box will be “YOUR TEXT”.

Similar to Method 1, the example showcases how you can chain methods in Python. By calling join().upper() directly, we convert and send the string as uppercase to the edit box in one line of code.

Summary/Discussion

  • Method 1: send_keys() with .upper(). Simplicity. Uses native Python string manipulation. May not handle edge cases well if additional formatting is needed.
  • Method 2: ActionsChains Class. Flexibility. Can simulate complex user interactions. More code for simple tasks.
  • Method 3: JavaScript Executor. Direct access. Bypasses some Selenium limitations. Requires familiarity with JavaScript. Potentially fragile if webpage scripts change.
  • Method 4: Keyboard Module. Mimics real user input. Useful for complex interactions. Requires keyboard module installation. May be less reliable in browser context.
  • Method 5: send_keys() with String Literal. Conciseness. Efficient for one-liners. Lacks clarity for those unfamiliar with method chaining.