[英]Selenium replace value for the input field with sendKeys() method
我對sendKeys()
方法有一個意外問題:
很長一段時間后一切正常,但是當代碼嘗試將數據設置到輸入字段時,意外地替換了 (certain(:)) 值:
例如,如果我在字段中設置值USER_NAME
,則值替換為/tmp/7d7b7...../upload123...file/USER_NAME
。 正如我們所看到的 - 一些路徑被添加到USER_NAME
值中。
我在方法中添加了日志,我們可以看到值被替換的時刻:
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
所以我們可以確定——值在值設置到字段中的那一刻准確設置。
重要的知識和結論:
並非所有用戶都被替換 - 只有幾個特定用戶。 所以我想一部分用戶被緩存了,但是我不明白發生這種情況的過程,為什么會發生這種情況。 以及這些用戶可能被緩存的位置。
我還重新啟動了docker,所以看來問題不在自動方面。
是否有可能通過后端或 UI 部分發生此問題?
看起來頁面上正在運行一個腳本,該腳本會更改您鍵入的輸入,因為這是一個密碼字段。
我建議您使用機器人 object 來模仿鍵盤敲擊。 首先使用 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());
}
根據 Logs 的說法,我認為有一些東西是有價值的。
建議嘗試:獲取更改的文本,進行一些操作,將其填回
string[] temp;
temp = (inputElement.Text).Split('/');
inputElement.Sendkeys(temp(temp.Length - 1));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.