I want to print the names of a search in google maps. I cant can find in HTML code the class where all the results from google maps are allocated. Im trying with the following line but it doesn't work
results = driver.find_elements(By.CLASS_NAME, 'section-result')
The code is the following:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
options = webdriver.ChromeOptions()
options.add_argument('--start-maximized')
options.add_argument('--disable-extensions')
driver_path = '/downloads/selenium/chromedriver.exe'
driver = webdriver.Chrome(driver_path, chrome_options=options)
driver.get('https://www.google.com/maps')
time.sleep(2)
# Accept coockies
element = driver.find_element(By.XPATH, "//button[.//span[text()='Aceptar todo']]")
element.click()
search_box = driver.find_element(By.NAME, 'q')
search_box.send_keys('clinics cork')
search_box.send_keys(Keys.RETURN)
time.sleep(2)
# From here it doesnt work
results = driver.find_elements(By.CLASS_NAME, 'section-result')
for result in results:
name = result.find_element(By.CLASS_NAME, 'section-result-title').text
print(name)
driver.quit()
I have also tried with the following line:
results = driver.find_element(By.XPATH, "//span[@jstcache='166']")
I dont obtain any error but the results variable its empty
Thanks!!
you can try this,
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
import time
options = webdriver.ChromeOptions()
options.add_argument('--start-maximized')
options.add_argument('--disable-extensions')
options.add_experimental_option("useAutomationExtension", False)
options.add_experimental_option("excludeSwitches", ["enable-automation"])
driver = webdriver.Chrome(options=options)
driver.get('https://www.google.com/maps')
time.sleep(1)
try:
# Accept coockies
element = driver.find_element(By.XPATH, "//button[.//span[text()='Aceptar todo']]")
element.click()
except NoSuchElementException:
pass
search_box = driver.find_element(By.NAME, 'q')
query = 'clinics cork'
search_box.send_keys(query)
search_box.send_keys(Keys.RETURN)
time.sleep(2)
results = driver.find_element(By.CSS_SELECTOR, f'div[aria-label="Results for {query}"]')
titles = results.find_elements(By.CSS_SELECTOR, 'div[class="NrDZNb"]')
for title in titles:
print(title.text)
output:
The Cork Clinic
Union Quay Medical Centre - Cork
MD Clinic
Sisu Clinic - Cork | Doctor-led, Aesthetic Medicine & Treatments
Doctor365 Lough Cork Walk-In, Out-Of-Hours & Online GP Services
Curraheen Clinic
Washington Street Medical Centre
City Clinic - Dental And Medical Clinic
The variable "results" holds the container of all the search outputs. And you can look for the details of all the individual search outputs inside this. since you're looking for the title of all search results, you can see how to do it. Similarly, you can also look for all other details for every individual search result.
Hope it's useful.