gpt4 book ai didi

java - 有关如何将数据键入移动应用程序的盒装空间的建议

转载 作者:行者123 更新时间:2023-12-02 08:53:13 25 4
gpt4 key购买 nike

我正在尝试按照以下屏幕截图模拟将数据输入到以下输入字段:enter image description here

盒装输入字段的编码如下

<div class="input-group otp-container">
<input type="tel" class="form-control form-input form-input--outline input-box-opt text-center text--md backupcode" style="" id="code-razer-8" maxlength="1" autocomplete="off" aria-required="true" name="OtpInfo.coderazer8" value="" required="">
<input type="tel" class="form-control form-input form-input--outline input-box-opt text-center text--md" id="code-razer-1" maxlength="1" autocomplete="off" required="required" aria-required="true" name="OtpInfo.coderazer1" value="">
<input type="tel" class="form-control form-input form-input--outline input-box-opt text-center text--md" id="code-razer-2" maxlength="1" autocomplete="off" required="required" aria-required="true" name="OtpInfo.coderazer2" value="">
<input type="tel" class="form-control form-input form-input--outline input-box-opt text-center text--md" id="code-razer-3" maxlength="1" autocomplete="off" required="required" aria-required="true" name="OtpInfo.coderazer3" value="">
<input type="tel" class="form-control form-input form-input--outline input-box-opt text-center text--md" id="code-razer-4" maxlength="1" autocomplete="off" required="required" aria-required="true" name="OtpInfo.coderazer4" value="">
<input type="tel" class="form-control form-input form-input--outline input-box-opt text-center text--md" id="code-razer-5" maxlength="1" autocomplete="off" required="required" aria-required="true" name="OtpInfo.coderazer5" value="">
<input type="tel" class="form-control form-input form-input--outline input-box-opt text-center text--md" id="code-razer-6" maxlength="1" autocomplete="off" required="required" aria-required="true" name="OtpInfo.coderazer6" value="">
<input type="tel" class="form-control form-input form-input--outline input-box-opt text-center text--md backupcode" style="" id="code-razer-7" maxlength="1" autocomplete="off" aria-required="true" name="OtpInfo.coderazer7" value="" required="">
</div>

例如,我尝试发送一行整数“453678”,这些数字填充在第一个框中,而不是每个框一个数字。

我想要实现的是,当我发送整数行时,它会自动用一个整数填充每个框。

目前,我使用以下方法来实现用单个整数填充每个框。

driver.findElement(By.xpath("//*[@id=\"code-razer-1\"]")).sendKeys(bkcd1);
System.out.println("the backupcode is : " + bkcd1);
Thread.sleep(2000);

driver.findElement(By.xpath("//*[@id=\"code-razer-2\"]")).sendKeys(bkcd2);
System.out.print("the backupcode is : " + bkcd2);
Thread.sleep(2000);

driver.findElement(By.xpath("//*[@id=\"code-razer-3\"]")).sendKeys(bkcd3);
System.out.print("the backupcode is : " + bkcd3);
Thread.sleep(2000);

driver.findElement(By.xpath("//*[@id=\"code-razer-4\"]")).sendKeys(bkcd4);
System.out.print("the backupcode is : " + bkcd4);
Thread.sleep(2000);

driver.findElement(By.xpath("//*[@id=\"code-razer-5\"]")).sendKeys(bkcd5);
System.out.print("the backupcode is : " + bkcd5);
Thread.sleep(2000);

driver.findElement(By.xpath("//*[@id=\"code-razer-6\"]")).sendKeys(bkcd6);
System.out.print("the backupcode is : " + bkcd6);
Thread.sleep(2000);

driver.findElement(By.xpath("//*[@id=\"code-razer-7\"]")).sendKeys(bkcd7);
System.out.print("the backupcode is : " + bkcd7);
Thread.sleep(2000);

driver.findElement(By.xpath("//*[@id=\"code-razer-8\"]")).sendKeys(bkcd8);
System.out.print("the backupcode is : " + bkcd8);
Thread.sleep(2000);

上面的方法可行,但我想减少代码行。

最佳答案

首先,将需要填充到输入框中的所有值存储在 HashMap 中。

Map<Integer, String> testData = new HashMap<>();
testData.put(1, "332133");
testData.put(2, "332123");
testData.put(3, "332143");
testData.put(4, "332167");
.....

找到相同类型的输入框,然后迭代循环。获取每个输入框的id属性,检查最后一个字符是否为数字,如果是则从HashMap中获取对应的数字值并填充到各自的文本框中。

List<WebElement> textBoxes = driver.findElements(By.cssSelector("input[id^='code-razer-']"));

for(WebElement element: textBoxes) {
String id = element.getAttribute("id");
char lastChar = id.charAt(id.length()-1);

if(Character.isDigit(lastChar)) {
element.sendKeys(testData.get(Character.getNumericValue(lastChar)));
}else {
// handle if element is different
}
Thread.sleep(1000);
}

关于java - 有关如何将数据键入移动应用程序的盒装空间的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60649484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com