I am making a scraper for Google Maps. The current code enters the page, accepts cookies, clicks the search bar and clicks enter to show the search results.
The current code should also click on the results and scroll down to show more (this part of the code):
scrolling_area = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="QA0Szd"]/div/div/div[1]/div[2]/div/div[1]/div')))
scrolling_area.click()
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
But the code doesn’t scroll down to load more results.
I have also found a similar topic on stackoverflow but this doesn’t work (the CSS selector is changed): SIMILAR QUESTION
What could be the problem?
Here is the full code:
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
import time
URL = "https://www.google.com/maps"
driver = webdriver.Chrome()
driver.maximize_window()
driver.get(URL)
# Accept cookies
try:
accept_cookies = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="yDmH0d"]/c-wiz/div/div/div/div[2]/div[1]/div[3]/div[1]/div[1]/form[2]/div/div/button')))
accept_cookies.click()
except NoSuchElementException:
print("No accept cookies button found.")
# Search for results and show them
input_field = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="searchboxinput"]')))
input_field.send_keys('catering uk')
input_field.send_keys(Keys.ENTER)
# Scroll to show more results
scrolling_area = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="QA0Szd"]/div/div/div[1]/div[2]/div/div[1]/div')))
scrolling_area.click()
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)
page_source = driver.page_source
driver.quit()
soup = BeautifulSoup(page_source, "html.parser")