當(dāng)我嘗試解析循環(huán)的最后一頁(yè)并在下一個(gè) (?) 按鈕不再存在時(shí)退出循環(huán)時(shí),出現(xiàn)以下錯(cuò)誤:selenium.common.exceptions.NoSuchElementException:消息:無(wú)法定位元素:?代碼: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()請(qǐng)給我一些關(guān)于當(dāng)元素不再存在時(shí)如何成功退出循環(huán)的指導(dǎo)。謝謝。
Python 中沒(méi)有錯(cuò)誤的中斷循環(huán) - Selenium
呼啦一陣風(fēng)
2023-12-29 15:36:08