51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

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

英文:

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

问题 {#heading}

我正在尝试点击这个链接中的所有产品。我想要点击所有的产品(点击第一个产品,返回,点击第二个产品,以此类推,直到最后一个产品)。但是当循环到第二个产品时,我遇到了一个"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}

得分: 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

赞(0)
未经允许不得转载:工具盒子 » selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: stale element not found