通过Robot Framework,有2种方法解析和获取 Web Table。
1)此脚本通过get text获取整行tr的数据,再进行处理
${Pgxxx_webList_tableBody_row_locator} //*[@class='xxx']/div[1]//*[@class='el-table__body']/tbody/tr
get_table_data_Pagexxx
${web_list} Get WebElements ${Pgxxx_webList_tableBody_row_locator}
${length} Get Length ${web_list}
${table_list} Create List
FOR ${i} IN RANGE 0 ${length}
${text} Get Text ${web_list}[${i}]
${list} Split String ${text} \n
Append To List ${table_list} ${list}
END
[Return] ${table_list}
#假设table body是一个3行3列:
${text}='a\nb\nc'
${text}='1\n2\n3'
${text}='d\ne\nf'
${list}=['a','b','c']
${list}=['1','2','3']
${list}=['d','e','f']
${table_list} = [['a','b','c'],['1','2','3'],['d','e','f']]
————————————————
版权声明:本文为优快云博主「levishan」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/levishan/article/details/121102709
2)此脚本通过获取每个td中的值后,形成一行的list,再把该list通过Append to List方法进行添加,形成新的list,依次类推。最后形成和1)一样的嵌套List,但是此方法缺点是处理时间长,因为它遍历的是td,而1)遍历的是tr(tr中包含多个td的值)
#1st keyword
get_table_data_pgxxxxx
${rowCount}= Get Element Count ${Pgxxxx_webList_tableBody_row_locator}
${colCount}= Get Element Count ${Pgxxxx_webList_tableBody_col_locator}
${list}= Create List
FOR ${rowIndex} IN RANGE 1 ${rowCount} + 1
${nested_loop_list}= Create List
nested_loop_get_table_data_Pgxxxxx ${rowIndex} ${colCount} ${nested_loop_list}
Append To List ${list} ${nested_loop_list}
${nested_loop_list} Set Variable ${EMPTY}
END
[Return] ${list}
#2nd keyword (被调用)
nested_loop_get_table_data_Pgxxxxx
[Arguments] ${rowIndex} ${colCount} ${nested_loop_list}
FOR ${colIndex} IN RANGE 1 ${colCount} + 1
${curText} Get Text //*[@class='el-table__body']/tbody/tr[${rowIndex}]/td[${colIndex}]
Append To List ${nested_loop_list} ${curText}
END