Larica B
Larica B

Reputation: 163

Selenium sendKeys are not sending all characters

I'm using Java, Selenium, and Chrome for test automation. Our developers recently upgraded our UI from AngularJS to Angular2 (not sure if that matters). But since then, sendKeys is inputting incomplete characters in to the text field. Here's an example:

    public void enterCustomerDetails()
    {   
        txtFirstName.sendKeys("Joh201605130947AM");
        txtSurname.sendKeys("Doe201605130947AM");
        txtEmail.sendKeys("[email protected]");
    }

I also tried using executeScript. It didn't work. It can enter complete characters but the form thinks the field is null.

public void forceSendKeys(WebElement element, String text)
{
    if (element != null)
        ((JavascriptExecutor) this.webDriver).executeScript("arguments[0].value=arguments[1]", element, text);
}

public void enterCustomerDetails()
        {   
            forceSendKeys(txtFirstName, "Joh201605130947AM");
            forceSendKeys(txtSurname, "Doe201605130947AM");
            forceSendKeys(txtEmail, "[email protected]");
        }

I also tried using .click() before .sendKeys and adding in sleep time. They didn't work too.

I got an idea to enter the characters 1 by 1 from this post: How to enter characters one by one in to a text field in selenium webdriver?

It worked but that means I have to rewrite all my codes from sendKeys to the new function:

    public void sendChar(WebElement element, String value)
{
    element.clear();

    for (int i = 0; i < value.length(); i++){
        char c = value.charAt(i);
        String s = new StringBuilder().append(c).toString();
        element.sendKeys(s);
    }       
}

public void enterCustomerDetails()
    {
        sendChar(txtFirstName, "Joh201605130947AM");
        sendChar(txtSurname, "Doe201605130947AM");      
        sendChar(txtEmail, "[email protected]");
    }

If you guys know a better way, please help! :)

Upvotes: 16

Views: 23822

Answers (9)

Dushyanth Sunny
Dushyanth Sunny

Reputation: 49

By using Actions class, this issue solved for me Tried many ways as mentioned above. Also tried to setvalue by js executescript Finally, found this code and it worked well for grid component built on angular

    actions.sendKeys(webElement,
    modifiedValue).perform();

Upvotes: 3

Yiannis
Yiannis

Reputation: 21

I had a similar issue for big texts in Java.

I overcome the issue using the copy and paste of the text in the keyboard-related methods, as in the following method:

import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;

public static void clickAndSetMultilineText(WebElement element, String text) {
    /** click on element **/
    element.click();

    /** clear older content of the text using keyboard functionality **/
    element.sendKeys(Keys.CONTROL + "a"); // select all text
    element.sendKeys(Keys.DELETE);        // delete old text
    
    StringSelection stringSelection= new StringSelection(text);
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.setContents(stringSelection, null); // copy text to the keyboard
    element.sendKeys(Keys.CONTROL+"v");           // paste text to the field
}

Hope this is helpful.

Upvotes: 0

S. Doe
S. Doe

Reputation: 785

Using

  • Chromium 78.0.3904.70,
  • Vaadin Flow Framework 14.1.3,
  • Selenium 3.141.59
  • and OpenJDK 11.0.5

the behavior also occurs and is even worse: I see that the character is typed in and suddenly after that it disappears. A workaround is to be persistent and just try it again. And again. Until the character is finally typed in.

    // Type in every single character
    for (int i = 0; i < textToType.length(); i++) {
        boolean typingCharacterWasSuccessful = false;
        // If typing was not successful before, just type again
        while (!typingCharacterWasSuccessful) {
            // Type in the character
            char singleCharacterToType = textToType.charAt(i);
            htmlTextfeld.sendKeys(Character.toString(singleCharacterToType));
            // Wait a little. Maybe alternatively/additionally wait.until(...)
            Thread.sleep(200);
            // Check typed in string.
            String inputValueAfterTyping = htmlTextfeld.getAttribute("value");
            if (inputValueAfterTyping.length() > i + 1) {
                // Alternatively: delete everything and start all over
                throw new Exception("Input value too long. Maybe character typed in twice!?");
            }
            // Typing was successful if the value in the input field is as expected (up to now)
            typingCharacterWasSuccessful
                    = inputValueAfterTyping.equals(textToType.substring(0, i + 1));
        }
    }

Upvotes: 0

i had the same problem, if you see it carefully selenium is changing the characters, some numbers perform a backspace or other symbols, i read it happens when using selenium with vncserver, i changed to firefox.... and it worked.

if that's not your problem, maybe sending the data in parts:

input1="Joh201605130947AM"
txtFirstName.sendKeys(input1[0:7])
txtFirstName.sendKeys(input1[8:end])

Upvotes: 0

user3379369
user3379369

Reputation:

This is due to a bug in Angular apps. Workaround is to put a sleep function.

public void setText(By element, String text) {
    sleep(100); // Angular version < 2 apps require this sleep due to a bug
    driver.findElement(element).clear();
    driver.findElement(element).sendKeys(text);
}

Upvotes: 0

Seunara
Seunara

Reputation: 185

I was getting this error too in Java, Selenium. You might also be getting this error too while writing your codes - "sendKeys (CharSequence) from the type Webelement refers to the missing type charSequence"

I tried varying the wait time and even typing extra characters before the main characters, they did not work.

The simple trick I used was to change the Java Compiler version from JRE 9 to JRE 10.

Upvotes: 0

Arg0n
Arg0n

Reputation: 8423

I stumbled upon this error when doing integration tests with NightwatchJS (which uses selenium).

So I'm writing this for people coming here in the future.

I wrote this extension command for nightwatch:

exports.command = function (selector, value, using) {
    var self = this;
    self.elements(using || 'css selector', selector, function (elems) {
        elems.value.forEach(function (element) {
            for (var c of value.split('')) {
                self.elementIdValue(element.ELEMENT, c);
            }
        });
    });
    return this;
};

Which can be used in this way:

var username = '[email protected]';
browser.setValueSlow('input[ngcontrol=username]', username); //Works with ng2!

This issue was also discussed on NightwatchJS's github here

Upvotes: 1

Manjunatha.N
Manjunatha.N

Reputation: 45

try this code.. other way to set values using javascript WebDriver driver = new FirefoxDriver(); JavascriptExecutor jse = (JavascriptExecutor)driver; jse.executeScript("document.getElementsByName('body')[0].setAttribute('type', 'text');"); driver.findElement(By.xpath("//input[@name='body']")).clear(); driver.findElement(By.xpath("//input[@name='body']")).sendKeys("Ripon: body text");

Upvotes: -3

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657238

I assume this is caused by this Angular2 issue https://github.com/angular/angular/issues/5808

Angular can't process input events when they arrive too fast.

As a workaround you would need to send single characters with a small delay between each.

Upvotes: 4

Related Questions