2
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
elem = driver.switch_to_active_element()
elem.send_keys('a')

I want to send keys to a currently active element on the page, but I don't know how to get the active element from driver. I need it because there is no name, id, class etc. on that element. I've found code for Java, something for Python(written above), but there's no result.

Here's the page, and the object "" with no attrs. How to select it?

<div action-name="menu-holder" class="uiMenuButtonSelectionHolder">
    <a href="javascript:;" class="choiceMenuClose" action-name="choice-menu-close"></a>
    <div style="top: 0px; left: 0px;" class="uiInlineBlock uiMenuHolder">
    <div>
       <input type="text">
    </div>
Obsidian
  • 515
  • 3
  • 10
Rastko Jović
  • 129
  • 1
  • 3
  • 10
  • Duplicate of [java - In Selenium how do I find the "Current" object - Stack Overflow](https://stackoverflow.com/questions/7491806/in-selenium-how-do-i-find-the-current-object) (somehow there's a Python solution there too) – user202729 Jan 07 '21 at 04:28

2 Answers2

19

I've been struggling with this myself until I came across this link.

switch_to_active_element() has been deprecated and we should be using switch_to.active_element

So, it should be:

elem = driver.switch_to.active_element
HenryM
  • 5,557
  • 7
  • 49
  • 105
0

You can identify the input tag through a css selector like below. Take a look at this to get a better understanding about selectors

elem = driver.find_element_by_css_selector('div.uiInlineBlock input')
elem.send_keys('a')

The selector can be read as find the input tag, descendant to the div with class name _uiInlineBlock_.

Hopefully, you don't have other divs with the same class, in which case you'll have to use find_elements_by_css_selector and from the list of elements returned, pick the element that you want to send_keys to.

Edit:

If the element is not visible, you can either go through the flow of making it visible or you can execute javascript to find and set the value

driver.execute_script("document.querySelector('div.uiInlineBlock input').setAttribute('value', 'a');")
Obsidian
  • 515
  • 3
  • 10
  • ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with – Rastko Jović Feb 11 '16 at 20:29
  • Have added how to handle hidden elements in the answer – Obsidian Feb 12 '16 at 05:12
  • Thanks! It works, but I have another problem. After that, I need to send enter key on that object In order to send value, but as I mentioned I can't access it in python. Is there any way to send enter also via JS, or have you any idea how to press enter after setting value. – Rastko Jović Feb 12 '16 at 20:30
  • @RastkoJović That will need another answer to explain. Take a look at this question [link] (http://stackoverflow.com/questions/3276794/jquery-or-pure-js-simulate-enter-key-pressed-for-testing) – Obsidian Feb 13 '16 at 04:34
  • If you're going with the native JS solution, the final step would be `document.querySelector('div.uiInlineBlock input').dispatchEvent(ev);` and if your page has jquery already loaded, then in the jquery solution `$('div.uiInlineBlock input').trigger(e);` – Obsidian Feb 13 '16 at 04:35