打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
集合用toString()强转成String取值各种报错
       
       当使用List、Map等集合时,取值给对象时,一定要特别注意,循环输出集合对象。

Q1.错误代码片段:
Map<String,String> map = new HashMap<String,String>; By by = null; if (map != null) { String property = map.keySet().toString();//Set集合 String value = map.values().toString();    debug时,鼠标移至对象名上面,可查看到取值情况,上述代码取到的值是“[cssSelector]”,把集合类型直接强转成String类型输出,这时,当然取不到正确的值;如下:
 分析:          需要通过迭代器循环输出数据 for (Iterator<?> propertyIterator = map.keySet().iterator();                                                                             propertyIterator.hasNext();) { String property = (String) propertyIterator.next(); }Q2.在使用ExcelReader读取excel文件内容时,存在有空的值
看这段报错信息,
             org.openqa.selenium.NoSuchElementException: 
Unable to locate element: {"method":"link text","selector":"[English, , , , , , ]"}Command duration or timeout: 30.24 secondsFor documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html

分析:     
          当excel存在多行数据,有一列有完整的值(图中“ExchangeDate”列),其余行空白格默认为空值存储。

 
     当代码读取到“International”时,看似只有一个值“English”,实际还有5行空数据,紧接着循环取值给findElement()的时候就会报错,找不到对应的element。
webDriver.findElement(By.xpath(expression)).click();

Q3.封装方法返回List<String>,当直接使用toString()取值给String字符串类型时,会出现如下情况:

Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"[English]"}For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.htmlBuild info: version: '2.45.0', revision: '5017cb8e7ca8e37638dc3091b2440b90a1d8686f', time: '2015-02-27 09:10:26'System info: host: 'Sunny-PC', ip: '172.27.216.1', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_67'Driver info: driver.version: unknown
<anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/g/AppData/Local/Temp/anonymous402472970410245538webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10271)
<anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///C:/Users/g/AppData/Local/Temp/anonymous402472970410245538webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:603)


分析:
          代码取到的值会默认加上一对“[ ]”,因为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);
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
JAVA版微信支付V3-完全版
C#中(int)、int.Parse()、int.TryParse()和Convert.ToInt32()的区别
C#,int转成string,string转成int
比较两个JSON不一样的地方,输出key
使用APT减少MVP的冗余代码
DOM4j使用教程
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服