WebDriver Element Click and Key Input Function

  • Share this:

Code introduction


This custom function takes a WebDriver instance, an element ID, and a string of keys to be sent, clicks on the element, and inputs the given keys.


Technology Stack : selenium

Code Type : Custom function

Code Difficulty : Intermediate


                
                    
def click_and_send_keys(driver, element, keys):
    try:
        # Click on the element to ensure it is visible and interactive
        driver.find_element_by_id(element).click()
        # Send keys to the element
        driver.find_element_by_id(element).send_keys(keys)
    except Exception as e:
        print(f"An error occurred: {e}")                
              
Tags: