The goal of this script is to access business names from an Excel file and then search it on google maps and scrape the required data. The issue is that the script is running correctly and scraping the data of the first business name on google maps. But when the second link loads and the data table occurs it throws some error.
How can I sort it out?
Feel free to run this script on your compiler. You can create an Excel file and write any two names in the a column of Excel file
from selenium import webdriver
from openpyxl import load_workbook
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options as ChromeOptions
import time
import pandas as pd
chrome_options = ChromeOptions()
Options = webdriver.ChromeOptions()
geckodriver_path = Service(r'E:\chromedriver_win32\chromedriver.exe')
driver = webdriver.Chrome(service=geckodriver_path, options=Options)
import pandas as pd
# Load the Excel workbook and select the sheet
workbook = load_workbook(r'C:\Users\muneeb\Desktop\Business name.xlsx')
sheet = workbook.active
# Create a list of business names from the first column of the sheet
business_names = [cell.value for cell in sheet['A']]
for business_name in business_names:
# Go to Google Maps
driver.get('https://www.google.com/maps/@24.9349012,67.2006144,15z')
# Find the search box and enter the business name
search_box = driver.find_element(By.ID, "searchboxinput")
search_box.send_keys(business_name)
# Find the first result and click on it
element = driver.find_element(By.CLASS_NAME, "mL3xi")
element.click()
# Wait for the business details to load
time.sleep(20)`
# Scrape the business details
name = driver.find_element(By.XPATH,"/html/body/div[3]/div[9]/div[9]/div/div/div[1]/div[2]/div/div[1]/div/div/div[2]/div[1]/div[1]/div[1]/h1/span[1]").text
phone = driver.find_element(By.XPATH,'/html/body/div[3]/div[9]/div[9]/div/div/div[1]/div[2]/div/div[1]/div/div/div[7]/div[5]/button/div[1]/div[2]/div[1]').text
website = driver.find_element(By.XPATH,'/html/body/div[3]/div[9]/div[9]/div/div/div[1]/div[2]/div/div[1]/div/div/div[7]/div[4]/a/div[1]/div[2]/div[1]').text
reviews = driver.find_element(By.XPATH,'/html/body/div[3]/div[9]/div[9]/div/div/div[1]/div[2]/div/div[1]/div/div/div[2]/div[1]/div[1]/div[2]/div/div[1]/div[2]/span[2]/span[1]/span').text
address = driver.find_element(By.XPATH,'/html/body/div[3]/div[9]/div[9]/div/div/div[1]/div[2]/div/div[1]/div/div/div[7]/div[3]/button/div[1]/div[2]/div[1]').text
print(len(phone),len(name),len(website),len(reviews),len(address))
This is the error message:
Traceback (most recent call last):
File "e:\Muneeb data\Webscraping\googlemaps_scraping.py", line 58, in <module>
phone = driver.find_element(By.XPATH,'/html/body/div[3]/div[9]/div[9]/div/div/div[1]/div[2]/div/div[1]/div/div/div[7]/div[5]/button/div[1]/div[2]/div[1]').text
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\muneeb\AppData\Roaming\Python\Python311\site-packages\selenium\webdriver\remote\webdriver.py", line 830, in find_element
return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\muneeb\AppData\Roaming\Python\Python311\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute
self.error_handler.check_response(response)
File "C:\Users\muneeb\AppData\Roaming\Python\Python311\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[3]/div[9]/div[9]/div/div/div[1]/div[2]/div/div[1]/div/div/div[7]/div[5]/button/div[1]/div[2]/div[1]"}