3
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>
1

2 Answers 2

20

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
Sign up to request clarification or add additional context in comments.

Comments

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');")

5 Comments

ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Have added how to handle hidden elements in the answer
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.
@RastkoJović That will need another answer to explain. Take a look at this question [link] (stackoverflow.com/questions/3276794/…)
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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.