当使用List、Map等集合时,取值给对象时,一定要特别注意,循环输出集合对象。 Q1.错误代码片段: debug时,鼠标移至对象名上面,可查看到取值情况,上述代码取到的值是“[cssSelector]”,把集合类型直接强转成String类型输出,这时,当然取不到正确的值;如下: 看这段报错信息, org.openqa.selenium.NoSuchElementException:
Unable to locate element: {"method":"link text","selector":"[English, , , , , ,
]"} Command duration or timeout: 30.24 seconds For documentation on this error, please visit: http:///exceptions/no_such_element.html 分析: 当excel存在多行数据,有一列有完整的值(图中“ExchangeDate”列),其余行空白格默认为空值存储。 当代码读取到“International”时,看似只有一个值“English”,实际还有5行空数据,紧接着循环取值给findElement()的时候就会报错,找不到对应的element。 webDriver.findElement(By.xpath(expression)).click(); Q3.封装方法返回List<String>,当直接使用toString()取值给String字符串类型时,会出现如下情况:
分析: 代码取到的值会默认加上一对“[ ]”,因为List<String>是一个集合,非String强类型,直接用exofactory.getAppDatas("International").toString();获取到的是集合String的值,eclipse看上去也不报错,但是,它是一个集合的值,集合相当于是一个存储空间,里面有多维空间可以存地址或数据。 当你需要获取集合所包含类型的值时,则需要通过循环或迭代出来集合里面存储的String类型值,才是正确的。 for (Iterator iterator = exofactory.getAppDatas("International").iterator(); iterator.hasNext();) { String type = (String) iterator.next(); System.out.println(type); } |
|
来自: _bolo > 《SeleniumRC》