python+selenium 树状结构列表遍历,展开所有+

首先找规律,我发现每个+的class=“ag-group-contracted”
然后找出这几个+,进行循环遍历
在这里插入图片描述
在这里插入图片描述

driver.switch_to.frame('ifContent')
path='//*[@class="ag-pinned-left-cols-container"]//*[@class="ag-group-contracted"]'
left_list=driver.find_elements_by_xpath(path)
for i in left_list:
    i.click()
    time.sleep(2)

结果报错:好像是说点击第一个+后,页面进行了刷新,找不到后边的+了。
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
在这里插入图片描述
解决办法是:
找到所有+,每次点击后重新定位,click第一个+,直到所有+都click

driver.switch_to.frame('ifContent')
path='//*[@class="ag-pinned-left-cols-container"]//*[@class="ag-group-contracted"]'
left_list=driver.find_elements_by_xpath(path)
print(len(left_list))
for i in range(len(left_list)):
    driver.find_elements_by_xpath(path)[0].click()
    time.sleep(2)

猜你喜欢

转载自blog.csdn.net/zhaoweiya/article/details/107022990
今日推荐