Reputation: 33
I'm facing a strange issue with the sendKeys method on my Selenium test. In my webapp I have a lot of inputs with default values, I get those inputs with a findElements() and then I try to fill them, very simple. To simplify, I have something like that:
List<WebElement> allInputs = driver().findElements(By.className("pouet"));
for (WebElement e : allInputs) {
e.clear();
e.sendKeys("pouet");
}
And it can fail because sometimes the sendKeys() fills the wrong input whereas the clear() has been correctly executed on the right input.
Does anyone has already faced this kind of issue?
Thanks a lot :)
Upvotes: 1
Views: 2411
Reputation: 446
I had a case where this exact thing happened. And adding the element.click()
call didn't help!
It turned out the culprit is not selenium. There was a script on the page that ised HTMLElement.focus()
to focus a particular element in the form depending on a condition.
So Selenium was actually racing with the JavaScript on the page for focus, and that caused this issue.
Upvotes: 0
Reputation: 7044
Encountered this, for my case sometimes my clear seemed to be failed and my sendKeys append the previous value. So I need to put Thread.sleep(1000) between e.clear() and e.sendKeys().
However based on your description it seems your issue is more to not able to point to the correct textfield. You should try to put the sleep after the sendKeys() like @acikojevic said.
Upvotes: 0
Reputation: 945
It is strange but it happened in my case, sometimes my firefoxdriver wrote in wrong field even if all fields were unique and successfully found. Small time span between two sendKeys() calls solved the issue. I wasn't using sleeps (you could try with Thread.sleep(5000)
just for test to ensure this is it), simple verification if correct text was written in the field between these 2 calls was enough time so that next sendKeys() writes in correct field.
Upvotes: 2