selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: stale element not found

huangapple go评论174阅读模式
英文:

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: stale element not found

问题

我正在尝试点击这个链接中的所有产品。我想要点击所有的产品(点击第一个产品,返回,点击第二个产品,以此类推,直到最后一个产品)。但是当循环到第二个产品时,我遇到了一个"stale element not found"错误。我尝试过隐式等待和time.sleep,但是无法解决。希望能得到帮助。

import time
from selenium import webdriver
from selenium.common import StaleElementReferenceException
from selenium.webdriver.common.by import By

chrome_options = webdriver.ChromeOptions()

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://foodkoreadubai.com/collections/beverages")
time.sleep(10)
try:
    popup_element = driver.find_element(By.CLASS_NAME, "popup-close")
    close_button = popup_element.find_element(By.CSS_SELECTOR, ".icon-close")
    close_button.click()
    time.sleep(4)
except StaleElementReferenceException:
    print("弹出窗口未弹出")
finally:
    box_product_elements = driver.find_elements(By.CSS_SELECTOR, ".box.product")
    i = 1
    last_product = len(box_product_elements)
    while i < last_product:
        box_product_elements[i].click()
        driver.back()
        i += 1
        time.sleep(2)
英文:

I am trying to click on all the products from this link. I wanted to click on all the products(click on the first product, come back, click on the second product, and so forth till the last product). But as soon as the loop reaches the second product, I get a stale element not found error. I've tried implicit wait, time. sleep. I couldn't solve it. Any help would be appreciated

import time
from selenium import webdriver
from selenium.common import StaleElementReferenceException
from selenium.webdriver.common.by import By

chrome_options = webdriver.ChromeOptions()

driver = webdriver.Chrome()
driver.maximize_window()
driver.get(&quot;https://foodkoreadubai.com/collections/beverages&quot;)
time.sleep(10)
try:
    popup_element = driver.find_element(By.CLASS_NAME, &quot;popup-close&quot;)
    close_button = popup_element.find_element(By.CSS_SELECTOR, &quot;.icon-close&quot;)
    close_button.click()
    time.sleep(4)
except StaleElementReferenceException:
    print(&quot;Pop up did not pop up&quot;)
finally:
    box_product_elements = driver.find_elements(By.CSS_SELECTOR, &quot;.box.product&quot;)
    i = 1
    last_product = len(box_product_elements)
    while i &lt; last_product:
        box_product_elements[i].click()
        driver.back()
        i += 1
        time.sleep(2)

答案1

得分: 1

你遇到了StaleElementReferenceException异常,因为你点击了driver.back(),导致产品页面重新渲染,之前的WebElements实例在新渲染的页面上不再存在。

你需要重新获取元素数组来处理这个问题。

# 你的代码

box_product_elements = driver.find_elements(By.CSS_SELECTOR, ".box.product")
i = 1
last_product = len(box_product_elements)
while i < last_product:
    driver.find_elements(By.CSS_SELECTOR, ".box.product")[i].click()
    driver.back()
    i+=1
英文:

You got StaleElementReferenceException, because you clicked driver.back() and page with product is re-rendered, so previous WebElements instances don't exist anymore on newly rendered page.

You need to get elements array again to handle this.

# your code

box_product_elements = driver.find_elements(By.CSS_SELECTOR, &quot;.box.product&quot;)
i = 1
last_product = len(box_product_elements)
while i &lt; last_product:
    driver.find_elements(By.CSS_SELECTOR, &quot;.box.product&quot;)[i].click()
    driver.back()
    i+=1

huangapple
  • 本文由 发表于 2023年8月11日 15:16:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76881422.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定