-1

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()
Alex Hsu
  • 9
  • 3

1 Answers1

-1

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

JaSON
  • 4,843
  • 2
  • 8
  • 15
  • Sorry if this is a stupid question, but how can I redefine 'next' each time without having to do it manually several times. I'd like to make this code work for several locations, not just NY so there will be a different number of pages every time. Therefore, it wouldn't work if I defined next manually each time. Is there a way to do it within the loop? – Alex Hsu Jun 30 '22 at 14:08
  • @AlexHsu you need to define `next` **inside** `while` loop – JaSON Jun 30 '22 at 14:20