简体   繁体   English

为什么即使元素存在于我的循环中,我也会收到 StaleElementReferenceException? (硒蟒)

[英]Why am I getting StaleElementReferenceException even though the element is present in my loop? (Selenium Python)

I'm pretty new to Python and coding in general, but I've been working on a datascraping project and I have been stuck for a couple days.总的来说,我对 Python 和编码还很陌生,但我一直在做一个数据抓取项目,而且我已经被困了几天。 Right now I am trying to make my code navigate through different pages in TripAdvisor.现在我正试图让我的代码在 TripAdvisor 的不同页面中导航。 The code allows me to move to the second page fine, but it has a problem moving to the third page and forward.该代码允许我很好地移动到第二页,但是移动到第三页并前进时出现问题。 I am trying to have it in a loop and I think that's where the main problem stems from.我试图让它循环起来,我认为这就是主要问题的根源。 If anyone could help out, that would be greatly appreciated.如果有人可以提供帮助,那将不胜感激。

My code so far:到目前为止我的代码:

import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import ElementNotInteractableException
from bs4 import BeautifulSoup
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import StaleElementReferenceException
from selenium.common.exceptions import ElementClickInterceptedException
import re
import pandas as pd
import time

URL = "https://www.tripadvisor.com/Hotels-g60763-New_York_City_New_York-Hotels.html"

class PythonOrgSearch(unittest.TestCase):
    
    def setUp(self):
        self.driver = webdriver.Chrome()

    def test_search_in_python_org(self):
        driver = self.driver
        driver.get(URL)
        self.assertIn("Hotel", driver.title)
        driver.execute_script("window.scrollTo(0, 3400)")
        time.sleep(2)
        see_all = driver.find_element(By.XPATH, '//*[@id="component_6"]/div/button')
        time.sleep(10)
        see_all.click()
        driver.execute_script("window.scrollTo(0, 11300)")
        time.sleep(10)
        #wait = WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a.nav.next.ui_button.primary")))

        next = driver.find_element(By.CSS_SELECTOR, "a.nav.next.ui_button.primary")
        here = next.is_displayed()
        while here == True:
            time.sleep(8)
            next.click()
            time.sleep(8)
            driver.execute_script("window.scrollTo(0, 11300)")
            time.sleep(10)
            if here != True:
                time.sleep(8)
                break
if __name__ == "__main__":
    unittest.main()

You're getting this exception because after you click on Next button new page is loaded (new DOM) and WebElement defined on previous page is no more valid (it is stale).您收到此异常是因为单击 Next 按钮后,新页面已加载(新 DOM),并且在前一页上定义的 WebElement 不再有效(它已过时)。 You need to re-define next on each loop iteration您需要在每次循环迭代时重新定义next

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我在页面上找到了StaleElementReferenceException,但仍然找到了该元素? - Why am I getting a StaleElementReferenceException even though it found the element on the page? 即使元素存在,也会发生 NoSuchElementException - Python Selenium - NoSuchElementException occurs even though the element is present - Python Selenium Python 和 Selenium 循环中的 StaleElementReferenceException - StaleElementReferenceException in for loop with Python & Selenium 为什么在 python flask 中使用 Selenium 时出现 No such element 异常? - Why am I getting a No such element exception using Selenium in python flask? 即使元素确实存在,我仍然在 selenium 中收到 NoSuchElementException - I keep getting a NoSuchElementException in selenium even though the element DOES exist 为什么我的计数器不更新,即使我在每个循环中都添加了一个? - Why does my counter not update even though I am adding one on every loop? 我正在尝试使用 selenium python 在 chrome 新选项卡中单击一个元素,但我收到错误 no such element 即使该元素存在 - I am trying to use selenium python to click an element in chrome new tab, but I am getting error no such element even thought the element is there 使用 selenium 对 influenster.com 执行 web 刮擦。 即使它在循环中并且 xpath 是正确的,我也只得到了一个刮掉的评论 - Performing web scraping using selenium on influenster.com. I am getting just one scraped review even though it was in loop and the xpath was correct 如何在 python 中使用 selenium 单击此按钮? 收到错误:StaleElementReferenceException - How can I click this button with selenium in python? getting error: StaleElementReferenceException 为什么我在 python 中的循环出现索引错误? - Why am I getting a index error for my loop in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM