![](/img/trans.png)
[英]Python Selenium StaleElementReferenceException while iterating through Select options
[英]StaleElementReferenceException when iterating through buttons/tiles on a page
我正在抓取https://www.bell.ca/Mobility/Smartphones_and_mobile_internet_devices ,当人们选择一部手机并继续选择计划作为“新客户”时,通常会有两个或三个按钮:“ premium ultra”,“ premium plus” ',等等。我已经为这些磁贴选择了css选择器,但是在进行第三次迭代时,我得到了'StaleElementReferenceException'。
我已经设置了try / exceptions,但是我希望遍历所有图块,因为我的代码的下一部分涉及遍历下面的“计划和数据选项”。
plantypes = driver.find_elements_by_css_selector('#prod-term-radio .hot-tile')
print(len(plantypes)) #number of plan types.
for plan in plantypes:
try:
plan.click()
time.sleep(3)
except exceptions.StaleElementReferenceException as e:
pass
我希望能够遍历所有图块,而不是在第二个图块过早地切掉。
单击后必须刷新元素列表,因为DOM
已更新,导致元素过时,请尝试此操作;否则,请执行以下操作。
plantypes = driver.find_elements_by_css_selector('#prod-term-radio .hot-tile')
print(len(plantypes)) #number of plan types.
for count, plan in enumerate(plantypes):
try:
ptypes = driver.find_elements_by_css_selector('#prod-term-radio .hot-tile')
ptypes[count].click()
time.sleep(3)
except exceptions.StaleElementReferenceException as e:
pass
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.