I am trying to scroll the webpage till all the elements are loaded. After a while my script gets failed.
I have tried to use different sleep time, but after scrolling a while items takes too long and selenium reached the bottom of the page. I want to wait untill the next element are loaded. here is my code
import time
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.edge.options import Options
# Specify the path to the ChromeDriver executable
chrome_driver_path = r'C:\Users\ansar\edgedriver\msedgedriver.exe'
# Create a new instance of the Chrome driver
driver = webdriver.Edge(options=edge_options)
driver.maximize_window()
# Navigate to the desired webpage
url = 'https://www.bigbasket.com/ps/?q=rice'
driver.get(url)
# Approach 1
while True:
driver.execute_script("window.scrollBy(0, 500);")
time.sleep(0.5)
#getting at the last of the page
try:
last = driver.find_element(By.XPATH,'//div[@class="w-full text-black text-center mt-10"]').text
if last == '- Thats all folks -':
print('we reached end of the page')
time.sleep(120)
break
except:
pass
# Approach 2
SCROLL_PAUSE_TIME = 10
last_height = driver.execute_script("return document.body.scrollHeight")
while True: # Scroll down to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Wait to load page
time.sleep(SCROLL_PAUSE_TIME)
# Calculate new scroll height and compare with last scroll height
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height