Google map error list indices must be integers or slices, not WebElement

Viewed 71

I am trying to scrape the phone number but they show me the error that your list must be integer not a web element I Have try different approches but they will show me these error this is the link https://www.google.com/maps/search/dentist+uk/@31.4820415,74.2444157,12z/data=!3m1!4b1

import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
import pandas as pd
options = webdriver.ChromeOptions()

# options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1920x1080")
options.add_argument("--disable-extensions")

chrome_driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=options)

data=[]
def supplyvan_scraper():
    with chrome_driver as driver:
        driver.implicitly_wait(15)
        URL = 'https://www.google.com/maps/search/dentist+uk/@31.5688259,74.2388013,12z/data=!3m1!4b1'
        driver.get(URL)
        time.sleep(3)
        page_links = [element.get_attribute('href') for element in driver.find_elements(By.XPATH, '//*[@class="hfpxzc"]')]

        # visit all the links
        for link in page_links:
            wev={}
            driver.get(link)
            time.sleep(2)
            link=driver.find_elements(By.XPATH, "//div[@class='RcCsl fVHpi w4vB1d NOE9ve M0S7ae AG25L']")
            for i in link:
                if '+' in link[i].get():
                    phone=driver.find_element(By.XPATH, "//div[@class='Io6YTe fontBodyMedium']").text
                    print(phone)


            time.sleep(2)
supplyvan_scraper()

Show error in these line:

if '+' in link[i]:

enter image description here

1 Answers

You can use regex to check the phone number and print it out easily

Phone number contains only digits and starting with a + sign So you may use this pattern - ^\+.*\d$

You can search all the text elements and find the number using this pattern

working code -

def google_map_scraper():
    with chrome_driver as driver:
        driver.implicitly_wait(15)
        URL = 'https://www.google.com/maps/search/dentist+uk/@31.5688259,74.2388013,12z/data=!3m1!4b1'
        driver.get(URL)
        time.sleep(3)
        page_links = [element.get_attribute('href') for element in
                      driver.find_elements(By.XPATH, '//*[@class="hfpxzc"]')]

        # visit all the links
        for link in page_links:
            driver.get(link)
            time.sleep(2)
            link = driver.find_elements(By.XPATH, "//div[@class='RcCsl fVHpi w4vB1d NOE9ve M0S7ae AG25L']")
            for i in link:
                if re.search('^\+.*\d$', i.text): # find the phone number starting with + and containing only digits
                    phone_num = i.text
                    print(phone_num) # print the phone number

            time.sleep(2)


google_map_scraper()

Output -

+92 334 5555859
+92 42 35749000
+92 322 3368251
+92 333 4254716
....
....
Related