Selenium - send_keys() sending incomplete string

后端 未结 3 708
说谎
说谎 2021-01-13 07:57

My problem: I have a method to fill a field, but the problem is that selenium is not sending the complete string to the field, so my assert fails at the tim

3条回答
  •  清歌不尽
    2021-01-13 08:05

    It looks like this is a common issue.

    Before trying the workarounds, as a sanity check, make sure that the input field is ready to receive input by the time you are sending keys. You could also try clearing the field before calling SendKeys. I am assuming that you are seeing your string truncated, and not characters missing or being prefixed with some artifact (like placeholder text or leftover input from a previous test).

    Some workarounds if that didn't work:

    1. Set the value of the input field using JavaScript, instead of calling SetKeys. On some websites where I do this, the input value actually won't be recognized unless I also trigger an input changed event.

      Example in C#. Hopefully, the only change you need is to make ExecuteScript be executeScript instead.

      driver.ExecuteScript("var exampleInput = document.getElementById('exampleInput'); exampleInput.value = '" + testInputValue + "'; exampleInput.dispatchEvent(new Event('change'));");
      

      You could, of course, split this up into two lines, one to set the value, and the second to dispatch the event.

    2. Send each key individually. This is a workaround I've seen a couple of times from the threads about this issue.

      for (var i = 0; i < first_name.length; i++) {
          name_field.sendKeys(first_name.charAt(i));
      }
      

    https://github.com/angular/protractor/issues/3196
    https://github.com/angular/protractor/issues/2019
    etc. etc. More threads can be found by a simple search of "webdriver sendkeys does not wait for all the keys" if you want to look for other possible solutions to your issue.

提交回复
热议问题