⌨️

Entering Text in Input Fields without `sendKeys`

Jul 12, 2024

Entering Text in Input Fields without sendKeys

Overview

  • Three methods for entering text without sendKeys:
    1. Action Class
    2. JavaScript Executor
    3. Robot Class

Method 1: Action Class

  • Create an action object.
  • Use the sendKeys method of Action Class (not Selenium's library class).
  • Steps:
    1. Create Action object.
    2. Find the input field element.
    3. Use the sendKeys method with the Action object.

Method 2: JavaScript Executor

  • Utilize JavaScript to directly set value of the input field.
  • Steps:
    1. Create a JavaScript executor.
    2. Find the input field element.
    3. Use JavaScript to set the value.

Method 3: Robot Class

  • Use Robot class to simulate keyboard operations.
  • Important Note: Works on the currently focused screen only.
  • Risk: If the test loses focus, text may be entered in a different application (e.g., Notepad).
  • Steps:
    1. Create Robot class object.
    2. Ensure the correct screen is visible and focused.
    3. Use Robot class methods to type text.

Practical Demonstration

  • Examples provided using Google Maps input field.
  • Testing all methods and verifying their functionality.
    • Action Class method: Verified and working.
    • JavaScript Executor method: Verified and working.
    • Robot Class method: Verified and working, but must ensure correct screen is focused.

Summary

  • These methods provide alternatives to sendKeys for entering text in Selenium.
  • Suitable depending on the specific needs and constraints of the application.