pythonseleniumsendkeys

Send keys without specifying element in python selenium webdriver (action chains did not work)


I have a script in which I click a button and the cursor begins blinking. All my attempts at specifying the element via xpath/id/class name to send the keys from have failed. So, I am attempting to just send the keys to where the cursor is blinking.

I've tried a few solutions:

  1. Send keys without specifying element in python selenium webdriver
from selenium.webdriver.common.action_chains import ActionChains

actions = ActionChains(driver)
actions.send_keys('dummydata')
actions.perform()

This solution did not send any keys at all.

  1. Switch to the active element.
elem = driver.switch_to.active_element()
elem['value'].send_keys('dummydata')
#OR#
elem.send_keys('dummydata')

In this instance, I received error:

elem = driver.switch_to.active_element()
TypeError: 'WebElement' object is not callable

Not sure what else to try at this point. Baffled at why this is happening.


Solution

  • One reason could be, that the element may inside an iframe as @Pedro is mentioning.

    To get the active element without XPath and other selectors, you can try using javascript:

    elem = driver.executeScript("document.activeElement")
    

    If this still does not work, it's either an iframe or maybe a virtual software textinput (js, canvas, ...). You can play around with some javascript commands in the browser console to check how and what elements are responding.