我有一个名为"inputIDD“的sitring,例如: String InputIDD= "input-2”;并希望将此字符串传递到其中:“input-2”的用法如下:
String InputIDD = "input-2";
for (int a = 1; a <= 6; a++)
{
System.out.println("Value of a:" + a);
System.out.println("Value of InputID:" + InputID);
// String y= Integer.toString(x);
switch (a) {
case 1:
driver.findElementByXPath("(//input[@id='input-2'])[1]").sendKeys("0");
break;
case 2:
driver.findElementByXPath("(//input[@id=InputIDD])[2]").sendKeys(ToAdd);
break; // optional
等,
driver.findElementByXPath("(//input[@id='input-2'])[1]").sendKeys("0");
:WORKS driver.findElementByXPath("(//input[@id=InputIDD])[1]").sendKeys("0");
:不工作
怎么了,有什么想法吗?
发布于 2018-09-11 04:19:31
在案例2中,将字符串InputIDD传递给xpath表达式,而不是变量InputIDD的值。您需要改为执行以下操作:
driver.findElementByXPath(String.format("(//input[@id=%s)[2]", InputIDD).sendKeys(ToAdd);
在上面的语句中,String.format方法将在格式字符串'%s‘表示的位置交换存储在InputIDD中的值。
https://stackoverflow.com/questions/52264855
复制相似问题