Solution 1 :

In the example app you provided, focus stays on the email input. You can check it using code below:

email = driver.find_element_by_id("email-input")
active_element = driver.execute_script("return document.activeElement")
assert active_element == email

You can try to use ActionChains, example:


Problem :

I have an HTML <input> field which produces autocomplete suggestions as the user types. I would like to make an automated test where the Selenium driver inputs some keys and then checks the contents of the autocomplete dropdown. The problem is that if I use the Selenium send_keys method, the <input> loses focus and the dropdown disappears. Is there a way to enter keys into an <input> element without losing the focus?

I made a minimalistic example:

from selenium import webdriver

class TestLogin():
    def setup_method(self, method):
        self.driver = webdriver.Chrome()
        self.vars = {}

    def teardown_method(self, method):

    def test_minimalistic_registration(self):

You can put a breakpoint on the last line and see that the focus of the <input> element is lost.


Comment posted by Rob Moll

Is setting focus back to the input in question a viable solution?

Comment posted by Martin Drozdik

@RobMoll unfortunately no. As soon as focus is lost, the autocomplete functionality is off.

Comment posted by Sers


Comment posted by Martin Drozdik

Thank you! I tried to switch to Firefox and it worked. Switched back to Chrome and now it works on Chrome as well. Must be a bug on my side.


Leave a Reply

Your email address will not be published. Required fields are marked *