So I have following element that I need to send keys to:
<input type="text" placeholder="Recipient address" value="">
but there are many elements like this on the page. If I search:
//input[@placeholder="Recipient address"]
I get 35 results (exact same element as above) while finding that XPATH, although, only one is active. The rest are hidden in the DOM. When I try to send keys, I get Element not interactable exception which is normal since there are multiple elements, and driver most likely tries to send keys to the wrong one. Then I tried to create following function:
def try_transfer():
while True:
try:
element = WebDriverWait(driver.instance, 2).until(ec.element_to_be_clickable((By.XPATH, '//input['
'@placeholder="Recipient address"]')))
element.is_enabled()
element.click()
element.send_keys('HG4sYqvkTfgBvGgZZhYfws4f8BoytTr1NmcDEwkKC2z8')
except TimeoutException:
break
I don't get the error white above function, but, it does not send keys to the right element.
Any idea how would I go about finding that specific active element and sending keys to it.
CodePudding user response:
You can use index for finding your particular data
If the index you're activating is in 1st position write this xpath
//input[@placeholder="Recipient address"][1].send_keys()
else
(//input[@placeholder="Recipient address"])[2].send_keys() # or 3 or 4 or 5 whatever you want
CodePudding user response:
As per the documentation activeElement()
switches to the element that currently has focus within the document currently "switched to", or the body element if this cannot be detected. This matches the semantics of calling "document.activeElement" in Javascript.
Similarly using using Selenium-Python clients if the desired element is the active element and having the focus you can use the following solution:
elem = driver.switch_to.active_element
elem.send_keys('HG4sYqvkTfgBvGgZZhYfws4f8BoytTr1NmcDEwkKC2z8')