Reputation: 139
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>
Upvotes: 3
Views: 31971
Reputation: 5793
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
Upvotes: 20
Reputation: 515
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');")
Upvotes: 0