1

I'm trying to send keys to an input box that is displayed within a dialog. The problem is that sometimes I get only the last few letters of my text, because the send_keys is sending them too fast.

I'm using Python 3.x, Selenium 3.x and working with chromedriver.

I already have a wait before sending keys:

my_input=WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'span label > input._58al')))
my_input.send_keys("Prueba")

I even added a second wait and a click before send_keys:

my_input=WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'span label > input._58al')))
WebDriverWait(self.driver, 10).until(EC.visibility_of(my_input)).click()
my_input.send_keys("Prueba")

Sometimes the text is being sent correctly. But sometimes I only get something like "ba" (from the whole text, which is "Prueba"):

input box

I'm trying to avoid using any explicit wait, but I've ran out of ideas.

  • 2
    Do some action before sendkeys, like clear the field, it will give little wait for script – Sachintha Aug 22 at 6:58
  • 2
    You could also try using the WebDriverWait to check whether the element is enabled, or one of the methods here: stackoverflow.com/questions/15122864/… – Kate Paulk Aug 22 at 11:55
2

If you want consistency, you're going to have to add a wait of some sort.

I always recommend explicit waits in "normal" scenarios, when it's obvious what to wait on. But in more complicated scenarios like this, using a quick sleep can ultimately be less of a headache than troubleshooting, writing, & maintaining an unusual/complicated/subtle explicit wait.

That said, I've encountered a similar problem before. Here's what I think is going on:

The dialog & text box load. After they're done loading, the textbox is automatically "selected". If this happens before or after Selenium enters text, no problem, but if it happens in the middle of Selenium entering text into the textbox, it ends up erasing part of the text. That's because when the textbox gets selected again, it causes the already-added text to be highlighted. Then Selenium adds the next char, overwriting the highlighted text instead of adding to it. The result is that it seems like only the 2nd half (or so) of the string gets entered into the textbox, like how you're seeing "ba" instead of the full "Prueba".

If this is what's happening and if you really want an explicit wait (I used to feel the same), maybe this will work for you:

In my case, I found a little-known Selenium attribute to be helpful: active_element. I don't remember the details but here's what I wrote down:

# The last step of loading the page is the login textbox becomes "active"
# (similar to but different from "selected").
# This change to "active" can mess up text entry, so we need to wait for it.
my_input = <selenium find>
WebDriverWait(driver, 5).until(
    # Strangely, there's no EC method to "wait until active", so we make our own
    lambda driver: driver.switch_to.active_element == my_input)
my_input.send_keys("Prueba")
  • While avoiding direct sleeps/waits in automated tests is a noble goal, it is worth noting that letting a test suite eat more time can often be a more cost effective long term solution than spending the time needed to 'make the tests perfect'. - You can waste your time or a computer's time, choose wisely. – TheLuckless Oct 11 at 22:06
  • @TheLuckless I agree except for the "often" part, maybe depending on what you meant. I rephrased my answer to emphasize what I feel is a good metric: if it's obvious what to wait on, wait on that. Otherwise, it's probably simpler to use a quick sleep. – mblakesley Oct 12 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.