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