You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Python Selenium捕获异常后重新执行前一行代码?

问题:处理爬取含下拉列表网站时的StaleElementReferenceException异常

我尝试爬取某网站的iPhone 11回收页面,页面包含两个需要操作的下拉列表(示意图如下)。
下拉列表示意图

此前我解决了下拉列表出现StaleElementReferenceException的问题,原因是需要先点击使元素可见。但捕获异常后,我需要WebDriver回到前一行代码重新点击元素,pass语句无法实现该需求。作为网页爬取新手,我不知该如何处理,以下是我的代码:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException, ElementNotInteractableException, StaleElementReferenceException
from time import sleep

options1 = webdriver.ChromeOptions()
options1.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options1)

link = "https://www.compareandrecycle.co.uk/mobile-phones/apple-iphone-11"

driver.get(link)
sleep(5)
cookie_button = driver.find_element("xpath", "//span[@class='cookie_acceptButton__gs5MX']").click()
for x in wait.until(EC.visibility_of_all_elements_located((By.XPATH,"//div[@class='storage-selectors']/span"))):
            size = x.text
            print(size)
            try:
                variants = x.click()
            except ElementNotInteractableException:
                pass
            
            network_options = driver.find_element("xpath", "//div[@class='network-options']//span[@class='selectboxit selectboxit-enabled selectboxit-btn']")
            network_options.click()
            sleep(3)
            for elem in driver.find_elements("xpath", "//div[@class='network-options']//div[@class='selectboxit-options selectboxit-list']/li"):
                try:
                    elem.click()
                except StaleElementReferenceException:
                    pass
                network = driver.find_element("xpath", "//div[@class='network-options']//span[@class='selectboxit selectboxit-enabled selectboxit-btn']/span[@class='selectboxit-text']").text
                print(network)
                
                sleep(3)
                condition_options = driver.find_element("xpath","//div[@class='condition-options']//span[@class='selectboxit selectboxit-enabled selectboxit-btn']")
                condition_options.click()
                sleep(3)
                for cons in driver.find_elements("xpath", "//div[@class='condition-options']//div[@class='selectboxit-options selectboxit-list']/li"):
                    try:
                        cons.click()
                    except StaleElementReferenceException: 
                        pass
                    condition = driver.find_element("xpath", "//div[@class='condition-options']//span[@class='selectboxit selectboxit-enabled selectboxit-btn']/span[@class='selectboxit-text']").text
                    print(condition)

内容的提问来源于stack exchange,提问作者Reggie18

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 03:55:20