我无法输入逻辑代码在动态文本字段中输入pin码。使用Selenium Webdriver Java
正在尝试在动态文本字段中输入pin码。每次加载网站时,Pin编号文本字段都会更改。需要帮助写入逻辑,以便在3文本字段中输入pin码。有4个管脚编号,但我可以选择在3文本字段中输入管脚编号我无法输入逻辑代码在动态文本字段中输入pin码。使用Selenium Webdriver Java,java,selenium,automated-tests,selenium-chromedriver,Java,Selenium,Automated Tests,Selenium Chromedriver,正在尝试在动态文本字段中输入pin码。每次加载网站时,Pin编号文本字段都会更改。需要帮助写入逻辑,以便在3文本字段中输入pin码。有4个管脚编号,但我可以选择在3文本字段中输入管脚编号 根据您发布的HTML。如果索引的字段类型相同,可以尝试使用索引 使用以下代码: driver.findElement(By.cssSelector("div[class='field-set'] input.input-pin:nth-child(1)")).sendKeys("your value"); /
根据您发布的HTML。如果索引的字段类型相同,可以尝试使用索引 使用以下代码:
driver.findElement(By.cssSelector("div[class='field-set'] input.input-pin:nth-child(1)")).sendKeys("your value"); // for first text box
driver.findElement(By.cssSelector("div[class='field-set'] input.input-pin:nth-child(2)")).sendKeys("your value"); // for Second text box
driver.findElement(By.cssSelector("div[class='field-set'] input.input-pin:nth-child(3)")).sendKeys("your value"); // for third text box
请更改div[class='field-set']输入中的索引
。输入pin:n子项(索引)
根据页面UI位置的文本框
更新
如果您不确定哪个文本框必须接受该值,请使用下面的逻辑
public static void main(String[] args) {
// first you need to store your testdata in a collection
Map<String,String> pinCodes = new HashMap<String, String>();
pinCodes.put("pin1", "2");
pinCodes.put("pin2", "3");
pinCodes.put("pin3", "3");
pinCodes.put("pin4", "4");
// you can remove the values as per you need suppose you only want `pin1` `pin2` then remove `pinCodes.put("pin3", "3");` and `pinCodes.put("pin4", "4");` from above code
enterPinCode(pincodes); //call method to enter the values in corresponding text-boxes
}
public void enterPinCode(Map<String,String> pinCodes) {
for (Entry<String, String> entry : pinCodes.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
switch (key) {
case "pin1" :
driver.findElement(By.cssSelector("div[class='field-set'] input.input-pin:nth-child(1)")).sendKeys(value);
break;
case "pin2" :
driver.findElement(By.cssSelector("div[class='field-set'] input.input-pin:nth-child(2)")).sendKeys(value);
break;
case "pin3" :
driver.findElement(By.cssSelector("div[class='field-set'] input.input-pin:nth-child(3)")).sendKeys(value);
break;
case "pin4" :
driver.findElement(By.cssSelector("div[class='field-set'] input.input-pin:nth-child(4)")).sendKeys(value);
break;
default :
System.out.println("pincode textbox key not found");
break;
}
}
}
此代码将在相应的pin码文本框中发送pin码(如果网页上有)。嗨,Narendra,我添加了详细代码的链接和图像。你能帮我一下吗code@DebanjanB,谢谢你对我的信任@BaluriAR使用基于文本的HTML更新问题上述代码有效,但我需要在3文本字段中输入pin码。由于是动态网站,pin文本字段每次都会更改。例如,我需要输入引脚1、引脚3、引脚4或1,2,3或4,3,1。我需要编写Java逻辑来执行引脚号。@Balurier,更新了答案。如果您还有其他疑问,请告诉我。每次您指的是网页上文本框的数量发生变化时,pin文本字段都会发生变化。有时它只显示2个文本框,有时显示4个文本框。是这样吗?或者元素ID正在更改?它有3个文本框,但元素ID正在更改。有关截图,请点击上面提到的截图和代码参考链接。这与动态ID无关。我使用了不同的定位器。更新后的答案对您无效吗?
switch (key) {
case "pin1" :
List<WebElement> box1 = driver.findElements(By.cssSelector("div[class='field-set'] input[placeholder='1st']"));
if(!box1.isEmpty()) {
box1.get(0).sendKeys(value);
}
break;
case "pin2" :
List<WebElement> box2 = driver.findElements(By.cssSelector("div[class='field-set'] input[placeholder='2nd']"));
if(!box2.isEmpty()) {
box2.get(0).sendKeys(value);
}
break;
case "pin3" :
List<WebElement> box3 = driver.findElements(By.cssSelector("div[class='field-set'] input[placeholder='3rd']"));
if(!box3.isEmpty()) {
box3.get(0).sendKeys(value);
}
break;
case "pin4" :
List<WebElement> box4 = driver.findElements(By.cssSelector("div[class='field-set'] input[placeholder='4th']"));
if(!box4.isEmpty()) {
box4.get(0).sendKeys(value);
}
break;
default :
System.out.println("pincode textbox key not found");
break;
}