[英]Selenium replace value for the input field with sendKeys() method
I have an unexpected issue with the sendKeys()
method:我对sendKeys()
方法有一个意外问题:
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:很长一段时间后一切正常,但是当代码尝试将数据设置到输入字段时,意外地替换了 (certain(:)) 值:
For example, if I set value USER_NAME
into the field, value replaced with /tmp/7d7b7...../upload123...file/USER_NAME
.例如,如果我在字段中设置值USER_NAME
,则值替换为/tmp/7d7b7...../upload123...file/USER_NAME
。 As we can see - some path was added into the USER_NAME
value.正如我们所看到的 - 一些路径被添加到USER_NAME
值中。
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: 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.我还重新启动了docker,所以看来问题不在自动方面。
Is it possible that this issue occurs via the backend or UI part?是否有可能通过后端或 UI 部分发生此问题?
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.我建议您使用机器人 object 来模仿键盘敲击。 First click on the text field using Selenium, then launch the Robot code (use package Java.awt):首先使用 Selenium 单击文本字段,然后启动机器人代码(使用 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.根据 Logs 的说法,我认为有一些东西是有价值的。
Suggest trying: Get the changed text, do some operation, fill it back建议尝试:获取更改的文本,进行一些操作,将其填回
string[] temp;
temp = (inputElement.Text).Split('/');
inputElement.Sendkeys(temp(temp.Length - 1));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.