0

Set value in the input element:

WebElement input = ...
input.sendKeys("1234989");

Sometimes, the input element only gets "1", not "1234989", any race condition here?

Another way:

Actions actions = new Actions(driver);
actions.sendKeys(input, "1234989").build().perform();

This one seems works better. What is the difference?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
eastwater
  • 4,624
  • 9
  • 49
  • 118

1 Answers1

0

The relevant HTML of the desired element would have helped us to debug why the desired element is populated only with 1 instead of 1234989. However as per best practices, while sending a character sequence to an input field you should always induce WebDriverWait for the elementToBeClickable() as follows:

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("css_input_element"))).sendKeys("1234989");

You can find a couple of relevant discussions in:


sendKeys(WebElement target, java.lang.CharSequence... keys)

sendKeys(WebElement target, java.lang.CharSequence... keys) is from the Actions class and is equivalent to calling Actions.click(element).sendKeys(keysToSend). This method is different from WebElement.sendKeys(CharSequence...).


public Actions sendKeys(java.lang.CharSequence... keys)

public Actions sendKeys(java.lang.CharSequence... keys) sends a CharSequence to the active element. Again this differs from calling WebElement.sendKeys(CharSequence...) on the active element in two ways:

  • The modifier keys included in this call are not released.
  • There is no attempt to re-focus the element, so sendKeys(Keys.TAB) for switching elements should work.

You can find a detailed discussion in When using Selenium's click_and_hold method exactly what conditions or actions cause the mouse click to release?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352