python - Selenium - Collect info from every item in a list -


sorry newbie question here, i'm trying learn web scraping working on yelp's eat24.com site. able 1) driver eat24.com, 2)choose pickup, 3) search location, 4) click on first menu , 5) collect menu items. i'm unable, however, go original list of restaurants , choose next menu in list. here code:

from selenium import webdriver import time selenium.webdriver.support.ui import webdriverwait selenium.webdriver.common.keys import keys  driver = webdriver.chrome()  #go eat24, type in zip code 10007, choose pickup , click search  driver.get("https://new-york.eat24hours.com/restaurants/index.php") search_area = driver.find_element_by_name("address_auto_complete") search_area.send_keys("10007") pickup_element = driver.find_element_by_xpath("//[@id='search_form']/div/table/tbody/tr/td[2]") pickup_element.click() search_button = driver.find_element_by_xpath("//*[@id='search_form']/div/table/tbody/tr/td[3]/button") search_button.click()   #scroll , down on page load more of 'infinity' list  in range(0,3):     driver.execute_script("window.scrollto(0,  document.body.scrollheight);")     driver.execute_script("window.scrollto(0,0);")     time.sleep(1)  #find menu buttons  menus_elements = driver.find_elements_by_xpath('//*[@title="view menu"]') #menus_element = driver.find_element_by_xpath('//*[@title="view menu"]') #menus_element.click()  #problem area: trying iterate on menu buttons , collect menu items + prices each. goes first menu , pulls prices/menu items, when goes first page says 'stale element reference' , doesn't click next menu item   in range(0, len(menus_elements)):     if menus_elements[i].is_displayed():         menus_elements[i].click()  #find menu items     menu_items = driver.find_elements_by_class_name("cpa")     menus = [x.text x in menu_items] #find menu prices     menu_prices = driver.find_elements_by_class_name('item_price')     menu_prices = [x.text x in menu_prices]         #pair menu items , prices     menu, menu_price in zip(menus, menu_prices):         print(menu + ': ' + menu_price)     driver.execute_script("window.history.go(-1)")     driver.implicitly_wait(20)  

the problem @ end here, goes first menu , grabs items/prices, when goes page, doesn't select second menu , same thing. why? thank , advice!!!

instead of clicking each "view menu" button, scraping menu page , getting result page can list of links , scrape each menu page 1 one:

menu_urls = [page.get_attribute('href') page in driver.find_elements_by_xpath('//*[@title="view menu"]')] url in menu_urls:     driver.get(url)     menu_items = driver.find_elements_by_class_name("cpa")     menus = [x.text x in menu_items]     ... 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -