How do I use selenium ChromeDriver to scroll the sidebar on Google maps to load more results?

Viewed 672

I’ve run into a problem trying to use Selenium ChromeDriver to scroll down the sidebar of a google maps results page. I am trying to get to the 6th result down but the result does not fully load until you scroll down. Using the find_element_by_xpath method, I am successfully able to access results 1-5 and click into them individually, but when trying to use the actions.move_to_element(link).perform() method to scroll to the 6th element, it does not work and throws an error message.

The error that I get is: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:

However, I know this element exists because when I manually scroll and more results are loaded, the Xpath works correctly. What am I doing wrong? I’ve spent many hours trying to solve this and I haven’t been able to solve with the available content out there. I appreciate any help or insights you can offer, thank you!

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup as soup
import time

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)

driver.get("https://www.google.com/maps")
time.sleep(7)
page = soup(driver.page_source, 'html.parser')

#find the searchbar, enter search, and hit return
search = driver.find_element_by_id('searchboxinput')
search.send_keys("dentists in Austin Texas")
search.send_keys(Keys.RETURN)
driver.maximize_window() 

time.sleep(7)

#I want to get the 6th result down but it requires a sidebar scroll to load
link = driver.find_element_by_xpath("//*[@id='pane']/div/div[1]/div/div/div[4]/div[1]/div[13]/div/a")

actions.move_to_element(link).perform()

link.click()

time.sleep(5)

driver.back()```
2 Answers

I found a solution that works, it is to target the element in XPATH from the javascript interface of selenium. You must then execute two commands on an instruction (targeting and scroll)

driver.executeScript("var el = document.evaluate('/html/body/jsl/div[3]/div[10]/div[8]/div/div[1]/div/div/div[4]/div[1]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; el.scroll(0, 5000);");

this is the only solution that worked for me

The search results in the google map are located with //div[contains(@aria-label,'dentists in Austin Texas')]//div[contains(@jsaction,'mouseover')] XPath.
So, to select 6-th element there you can do the following

from selenium.webdriver.common.action_chains import ActionChains

results = driver.find_elements_by_xpath('//div[contains(@aria-label,"dentists in Austin Texas")]//div[contains(@jsaction,"mouseover")]')

ActionChains(driver).move_to_element(results[6]).click(button).perform()
Related