0

my process is there is a list of restaurants and i want to click on it and do some stuff and come back and same with the next restaurants in the list using a loop as you can see in below image i followed this link but didn't get clarification

enter image description here

CODE

import time
from selenium import webdriver
from selenium.webdriver.common.by import By


driver = webdriver.Chrome(executable_path='../chromedriver.exe')
driver.get("https://www.zomato.com/kolkata/dine-out?dishv2_id=76487754bd59c594cd5218d3427e68e0_2&rating_range=4.0-5.0")
screen_height = driver.execute_script("return window.screen.height;")  # get the screen height of the web
i = 1
count = 0
scroll_pause_time = 1

while True:
    # scroll one screen height each time
    driver.execute_script("window.scrollTo(0, {screen_height}*{i});".format(screen_height=screen_height, i=i))
    i += 1
    time.sleep(scroll_pause_time)   
    # update scroll height each time after scrolled, as the scroll height can change after we scrolled the page
    scroll_height = driver.execute_script("return document.body.scrollHeight;")
    # Break the loop when the height we need to scroll to is larger than the total scroll height
    if (screen_height) * i > scroll_height:
        break

driver.execute_script("window.scrollTo(0, 0);")
res_list = []
titles = driver.find_elements_by_xpath('//a[@class="sc-dBAPYN kcrxQo"]')

#this is block of code which is error prone

for i in titles:
    time.sleep(5)
    driver.execute_script("window.scrollTo(0, 0);")
    element = i.find_element_by_xpath('./div/h4')
    driver.execute_script("arguments[0].click();", element)
    time.sleep(3)
    name_of_rests = driver.find_element_by_css_selector('#root > div > main > div > section.sc-kxynE.jzTfFZ > section > section > div > div > div > h1').text
    res_list.append(name_of_rests)

    driver.back()

print(res_list)
driver.close()
  • Plz give more info - for which line of code/element you getting stale ref exception> In general to avoid stale ref - you shall try getting element ref ( by its appropriate locator) as soon as you are ready to perform operation on it. – Satyendra Sharma Aug 26 '21 at 10:23

1 Answers1

1

You have to define this

 driver.find_elements_by_xpath('//a[@class="sc-dBAPYN kcrxQo"]')

again in loop.

Code :

number_of_titles = len(driver.find_elements_by_xpath('//a[@class="sc-dBAPYN kcrxQo"]'))

#this is block of code which is error prone
j = 0 
for i in range(number_of_titles):
    time.sleep(5)
    titles = driver.find_elements_by_xpath('//a[@class="sc-dBAPYN kcrxQo"]')
    driver.execute_script("window.scrollTo(0, 0);")
    element = titles[j].find_element_by_xpath('.//div//h4')
    driver.execute_script("arguments[0].click();", element)
    time.sleep(3)
    j = j +1
    name_of_rests = driver.find_element_by_css_selector('#root > div > main > div > section.sc-kxynE.jzTfFZ > section > section > div > div > div > h1').text
    res_list.append(name_of_rests)

    driver.back()
cruisepandey
  • 28,520
  • 6
  • 20
  • 38