警报将为每个迭代打印[object]
,而不是elemID
的值。
for (WebElement el : listOfElem){
String elemID = el.getAttribute("id");
Javascriptexecutor js = (Javascriptexecutor) driver;
String strg = (String) js.executeScript("alert("+elemID+");");
}
我还尝试了下面的代码:
String strg = (String)js.executeScript("alert("+elemID+");").toString();
这也会打印[object]
(只需注意:使用toString()
会中断循环,不确定原因)
如何在每次迭代中打印elemID
的值?
如果我在JavaScript之外打印elemID
,它会打印值,但我希望它在JavaScript内。
编辑:如果我这样做
for (WebElement el : listOfElem){
String elemID = el.getAttribute("id");
System.out.println(elemID);
Javascriptexecutor js = (Javascriptexecutor) driver;
String strg = (String) js.executeScript("alert("+elemID+");");
System.out.println(strg);
}
它会正确地打印第一个System.out的值,但其余的迭代都会打印null
如果我按照评论中的建议使用下面的
上面编辑的代码中的String strg = (String) js.executeScript("console.log("+elemID+");");
它打印值(第一个Sys.out),然后就是它,之后它中断循环并输出。
发布于 2018-08-04 06:37:45
如果有人正在寻找答案,我可以通过以下方式找到答案:
for (WebElement el : listOfElem){
String elemID = el.getAttribute("id");
System.out.println(elemID); //1st
Javascriptexecutor js = (Javascriptexecutor) driver;
Object strg = js.executeScript("return(arguments[0]);",elemID);
System.out.println(strg); //2nd
}
上面的代码在每次迭代中打印第二条Sys.out语句中elemID
的值,该语句来自javascript
发布于 2018-08-04 03:31:38
您需要直接在elemID
上调用toString()
。
请尝试以下代码:
for (WebElement el : listOfElem){
String elemID = el.getAttribute("id");
Javascriptexecutor js = (Javascriptexecutor) driver;
String strg = (String) js.executeScript("alert("+elemID.toString()+");");
}
https://stackoverflow.com/questions/51678883
复制相似问题