I need my selenium webdriver to wait a bit more, but don't want to add an explicit wait
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"):
I'm trying to avoid using any explicit wait, but I've ran out of ideas.
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
sleepcan 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")