亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python 中沒有錯誤的中斷循環 - Selenium

Python 中沒有錯誤的中斷循環 - Selenium

呼啦一陣風 2023-12-29 15:36:08
當我嘗試解析循環的最后一頁并在下一個 (?) 按鈕不再存在時退出循環時,出現以下錯誤:selenium.common.exceptions.NoSuchElementException:消息:無法定位元素:?代碼:while True:    prices = driver.find_element_by_id('showAllGraphsButton')    prices.click()    time.sleep(6)    s_container = driver.find_element_by_id('stockContainer')    stocks = WebDriverWait(s_container, 10).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "imageRow")))    for stock in stocks:        name = WebDriverWait(stock, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, "auto-ellipsis"))).text        stock_name.append(name)        percentage = WebDriverWait(stock, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, "return-value"))).text        stock_percentage.append(percentage)        price = WebDriverWait(stock, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, "last-price"))).text        stock_price.append(price)        if len(driver.find_element_by_link_text('?').text) > 0:            driver.find_element_by_link_text('?').click()            time.sleep(6)        else:            breakdriver.quit()請給我一些關于當元素不再存在時如何成功退出循環的指導。謝謝。
查看完整描述

1 回答

?
泛舟湖上清波郎朗

TA貢獻1818條經驗 獲得超3個贊

試試這個:在代碼中添加“try”和“ except”函數。使用它們,代碼不會返回錯誤,但會停止。如果你愿意,你可以在出錯時重復循環,你只能在 try 結束時放置break。while True: 嘗試: #some 代碼中斷 except: #some 代碼



查看完整回答
反對 回復 2023-12-29
  • 1 回答
  • 0 關注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號