![](/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.