問題描述:下拉框選擇了某個值去查詢,查詢後將下拉框復原爲默認值,出現了異常提醒。
1、代碼及報錯
代碼
def type_query(self,target_loc,row):
try:
ele_sel = Select(self.find_element(*target_loc))
ele_sel.select_by_index(row)
sleep(0.2)
self.find_element(*self.BaImp_loc).click()
sleep(2)
ele_sel.select_by_index(0)
except BaseException as msg:
print(msg)
報錯信息
Message: The element reference of <select id="state" name="state"> is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed
2、解決方法
點擊查詢後,頁面被刷新,ele_sel已失效,需要重新獲取。
def type_query(self,target_loc,row):
try:
ele_sel = Select(self.find_element(*target_loc))
ele_sel.select_by_index(row)
sleep(0.2)
self.find_element(*self.BaImp_loc).click()
sleep(2)
ele_sel0 = Select(self.find_element(*target_loc))#重新獲取
ele_sel0.select_by_index(0)
except BaseException as msg:
print(msg)