即使我的循环中存在该元素,我为什么还会得到StaleelementReferenceException? (Selenium Python)

发布于 2025-02-11 20:30:22 字数 2021 浏览 1 评论 0原文

我是Python和编码的新手,但是我一直在研究一个数据捕捉项目,并且已经被困了几天了。现在,我正在尝试使我的代码在TripAdvisor中的不同页面中导航。该代码使我可以罚款第二页,但是它有一个问题移至第三页并向前移动。我试图将其循环融合,我认为这是主要问题的位置。如果有人能提供帮助,这将不胜感激。

到目前为止,我的代码:

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()

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. Right now I am trying to make my code navigate through different pages in 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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水染的天色ゝ 2025-02-18 20:30:22

您会得到此例外,因为在单击下一个按钮后,加载了新页面(新DOM),并且上页上定义的WebElement不再有效(这是陈旧)。您需要在每个循环迭代中重新定义下一个

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). You need to re-define next on each loop iteration

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文