简体   繁体   中英

Selenium replace value for the input field with sendKeys() method

I have an unexpected issue with the sendKeys() method:

A long time before it all worked fine, but unexpectedly the (certain(:)) values are replaced when the code tries to set data into the input field:

在此处输入图像描述

For example, if I set value USER_NAME into the field, value replaced with /tmp/7d7b7...../upload123...file/USER_NAME . As we can see - some path was added into the USER_NAME value.

I added logs to the method and we can see a moment when the value was replaced:

    clearInputFld(inputFld);
    Log.info("INSIDE clearAndTypeIntoInputField() ---------> value after clearing: " + inputElement.getAttribute("value"));
    Log.info("INSIDE clearAndTypeIntoInputField() ---------> value to set: " + value);

    inputElement.sendKeys(value);

    Log.info("INSIDE clearAndTypeIntoInputField() ---------> value after set: " + inputElement.getAttribute("value"));

Output:

INSIDE clearAndTypeIntoInputField() ---------> value after clearing: 
INSIDE clearAndTypeIntoInputField() ---------> value to set: USER_NAME
INSIDE clearAndTypeIntoInputField() ---------> value after set: /tmp/7d7b7...../upload123...file/USER_NAME

So we can be sure - value sets exactly at the moment when value sets into the field.

Important to know, and conclusions:

  • Not all users replaced - Only several certain users. So I suppose a part of users is cached, But I do not understand the process with which this happens, why this happens. and where these users might be cached.

  • I also restarted the docker, so it seems the problem is not in the automatic side.

  • Is it possible that this issue occurs via the backend or UI part?

It looks like there is a script running on the page that changes the input you type, as this is a password field.

What I suggest is that you use the Robot object to mimic keyboard strokes. First click on the text field using Selenium, then launch the Robot code (use package Java.awt):

Robot robot = null;
    try {
        robot = new Robot();            
        for (char c : textToType.toCharArray()) {
            int keyCode = KeyEvent.getExtendedKeyCodeForChar(c);
            if (KeyEvent.CHAR_UNDEFINED == keyCode) {
                logger.error("Key code not found for character '" + c + "'");
            } else {
                try {
                    robot.keyPress(keyCode);
                    robot.delay(10);
                    robot.keyRelease(keyCode);
                    robot.delay(10);
                } catch (Exception e) {
                    if (c == '_') {
                        robot.keyPress(KeyEvent.VK_SHIFT);
                        robot.keyPress(KeyEvent.VK_MINUS);
                        robot.keyRelease(KeyEvent.VK_MINUS);
                        robot.keyRelease(KeyEvent.VK_SHIFT);
                    }
                    if (c == ':') {
                        robot.keyPress(KeyEvent.VK_SHIFT);
                        robot.keyPress(KeyEvent.VK_SEMICOLON);
                        robot.keyRelease(KeyEvent.VK_SEMICOLON);
                        robot.keyRelease(KeyEvent.VK_SHIFT);
                    }
                }
            }
        }
        robot.keyPress(KeyEvent.VK_ENTER);           
    } catch (Exception ex) {
        logger.error(ex.getMessage());
    }

According to Logs, I think there is something come with the value.

Suggest trying: Get the changed text, do some operation, fill it back

string[] temp;
temp = (inputElement.Text).Split('/');
inputElement.Sendkeys(temp(temp.Length - 1));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM