循环dict操作中的一些常用问题


check_td = {'TargetApplication': TargetApplication,'UserId': UserId, 'UserName': UserName,
'CreatedDate': date, 'LastRetryDate': LastRetry_Date, 'NumberofRetries': NumberofRetries,
'IsActive':IsActive}
for th in check_td:
td_value = action.get_element_text((By.XPATH, f'//table/tbody/tr/td[{list(check_td.keys()).index(th) + 1}]'))

问题:获取th在xpath中的索引了,td同理

思路:字典中的key是按照表中的th的顺序写的,根据key获取到index的值,index+1就是表中的th从左到右的顺序的index,就是xpath中的索引了
case1:
list(check_td.keys()).index(th) + 1, 必须要把dict_keys 转换成 list
check_td.keys(), ---type:dict_keys, 这个类型没有index()
list(check_td.keys()) ---type: list, 转换成list之后就有了
list(check_td.keys()).index()+1 ---根据th取到下标,就是取到索引,取到之后+1就是xpath中的index了

 


posted @ 2022-03-04 14:41  苍耳冬阳  阅读(78)  评论(0)    收藏  举报