請建議我如何回圈 tr[1]、[2]、[3] 或 N 個數字,如果頁面在回圈 1 后自動重繪 并且不想繼續回圈 2、回圈,則按照網站上提供的表格自動執行3、回圈N?
例如:回圈 1(頁面自動重繪 )> 回圈 2(頁面自動重繪 )> 回圈 N(頁面自動重繪 )| *此自動重繪 頁面直接來自網站,而非代碼
代碼:
try:
while True:
browser.get('URL')
options = browser.find_elements_by_xpath('//*[@id="event"]/tbody/tr')
for opt in options:
opt.find_element_by_xpath('./td[9]').click()
time.sleep(2.5)
break
except (ElementNotVisibleException, WebDriverException, NoSuchElementException):
pass
這里是一個while回圈連續迭代時for回圈已經達到了網站,我建議有在一個錯誤的回圈N環路,但我不知道它在哪里,這樣我可以繼續回圈
uj5u.com熱心網友回復:
假設頁面click在對td標簽進行操作后重繪 ,對于下一次迭代,元素options不再被找到(因為頁面被重繪 了)。
嘗試options在每次迭代中查找元素,以便即使在頁面重繪 后也能找到該元素。
try:
while True:
browser.get('URL')
options = browser.find_elements_by_xpath('//*[@id="event"]/tbody/tr')
for i in range(len(options)):
options = browser.find_elements_by_xpath('//*[@id="event"]/tbody/tr')
options[i].find_element_by_xpath('./td[9]').click()
time.sleep(2.5)
break
except (ElementNotVisibleException, WebDriverException, NoSuchElementException):
pass
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/338306.html
