Get city name from a list of raw address with selenium
Question:
I have a list of address
list_x = ['A', 'B', 'C', 'D']
and i want to send these value into google map search one by one.
My code here:
from selenium import webdriver
import time
from time import sleep
path = r"C:/Users/admin/chromium-browser/chromedriver.exe"
options = webdriver.ChromeOptions()
options.binary_location = r"C:Userssonpn.vbiAppDataLocalGoogleChromeApplicationchrome.exe"
driver = webdriver.Chrome(path, chrome_options=options)
driver.get("https://www.google.co.in/maps/@10.8091781,78.2885026,7z")
sleep(2)
for index, item in enumerate(list_x, start=0):
Place = driver.find_element("class name", "tactile-searchbox-input").send_keys(item)
Submit = driver.find_element("xpath", "//*[@id='searchbox-searchbutton']").click()
result = driver.find_elements("xpath", '//*[@id="QA0Szd"]/div/div/div[1]/div[2]/div/div[1]/div/div/div[2]/div[1]/div[1]')
convert1 = [el.text for el in result]
convert.extend(convert1)
time.sleep(3)
close = driver.find_element('xpath', '//*[@id="sb_cb50"]').click()
and the error is
WebDriverException: Message: chrome not reachable
(Session info: chrome=97.0.4692.71)
Please help me with this error. Thank you
Answers:
This error message…
WebDriverException: Message: chrome not reachable (Session info: chrome=97.0.4692.71)
…implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context i.e. google-chrome session.
Your main issue is the incompatibility between the version of the binaries you are using as follows:
- Possibly you are using the latest chrome=104.0
- But you are using chromedriver=97.0
So there is a clear mismatch between chromedriver=97.0 and the chrome=104.0
Solution
Ensure that:
- Selenium is upgraded to current released Version 4.4.0.
- ChromeDriver is updated to current ChromeDriver v104.0 level.
- Chrome Browser is updated to current chrome=104.0 (as per chromedriver=104.0 release notes).
As an alternative solution, you can use API, such as Google Maps Place Results API from SerpApi (paid API with a free plan that handles blocks and parsing on their backend).
To begin with, we create a list of the necessary addresses and a list with coordinates to them (the coordinates can be found in the URL of the address we need):
# addresses from which we want to extract the name of the cities
addresses = [
'Bälliz 22, Switzerland',
'Blümlisalpstrasse 36, Switzerland',
'Stauffacherstrasse 105, Switzerland',
'Am Wasser 3, Switzerland',
'Ringstrasse 18, Switzerland'
]
# GPS coordinates of location where you want your q (query) to be applied
# those coordinates are taken from the Google Maps URL
geo_coordinates = [
'@46.7600484,7.6155472,14.08z',
'@46.7810488,7.574276,14z',
'@47.3792421,8.5218228,16z',
'@47.4039247,8.5970111,16z',
'@47.4139972,9.190949,13z'
]
Next, we loop through the lists using zip()
, get the full address from API response and use regular expression to extract the name of the city:
city_name = re.search(r'd+s(w+),', results['place_results']['address']).group(1)
Check full code in online IDE.
from serpapi import GoogleSearch
import json, re, os
# addresses from which we want to extract the name of the cities
addresses = [
'Bälliz 22, Switzerland',
'Blümlisalpstrasse 36, Switzerland',
'Stauffacherstrasse 105, Switzerland',
'Am Wasser 3, Switzerland',
'Ringstrasse 18, Switzerland'
]
# GPS coordinates of location where you want your q (query) to be applied
geo_coordinates = [
'@46.7600484,7.6155472,14.08z',
'@46.7810488,7.574276,14z',
'@47.3792421,8.5218228,16z',
'@47.4039247,8.5970111,16z',
'@47.4139972,9.190949,13z'
]
for address, coordinates in zip(addresses, geo_coordinates):
params = {
"api_key": "...", # serpapi key, https://serpapi.com/manage-api-key
"engine": "google_maps", # SerpApi search engine
"type": "search", # list of results for the query
"google_domain": "google.com", # google domain
"q": address, # query
"hl": "en", # language
"ll": coordinates # GPS coordinates
}
search = GoogleSearch(params) # where data extraction happens on the backend
results = search.get_dict() # JSON -> Python dict
city_name = re.search(r'd+s(w+),', results['place_results']['address']).group(1)
print(f'City name: {city_name}')
Example output:
City name: Thun
City name: Heimberg
City name: Zürich
City name: Dübendorf
City name: Neuenhof
There’s a Using Google Maps Place Results API from SerpApi using Python blog post if you need a little bit more code explanation and how to extract other data.
Disclaimer, I work for SerpApi.
I have a list of address
list_x = ['A', 'B', 'C', 'D']
and i want to send these value into google map search one by one.
My code here:
from selenium import webdriver
import time
from time import sleep
path = r"C:/Users/admin/chromium-browser/chromedriver.exe"
options = webdriver.ChromeOptions()
options.binary_location = r"C:Userssonpn.vbiAppDataLocalGoogleChromeApplicationchrome.exe"
driver = webdriver.Chrome(path, chrome_options=options)
driver.get("https://www.google.co.in/maps/@10.8091781,78.2885026,7z")
sleep(2)
for index, item in enumerate(list_x, start=0):
Place = driver.find_element("class name", "tactile-searchbox-input").send_keys(item)
Submit = driver.find_element("xpath", "//*[@id='searchbox-searchbutton']").click()
result = driver.find_elements("xpath", '//*[@id="QA0Szd"]/div/div/div[1]/div[2]/div/div[1]/div/div/div[2]/div[1]/div[1]')
convert1 = [el.text for el in result]
convert.extend(convert1)
time.sleep(3)
close = driver.find_element('xpath', '//*[@id="sb_cb50"]').click()
and the error is
WebDriverException: Message: chrome not reachable
(Session info: chrome=97.0.4692.71)
Please help me with this error. Thank you
This error message…
WebDriverException: Message: chrome not reachable (Session info: chrome=97.0.4692.71)
…implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context i.e. google-chrome session.
Your main issue is the incompatibility between the version of the binaries you are using as follows:
- Possibly you are using the latest chrome=104.0
- But you are using chromedriver=97.0
So there is a clear mismatch between chromedriver=97.0 and the chrome=104.0
Solution
Ensure that:
- Selenium is upgraded to current released Version 4.4.0.
- ChromeDriver is updated to current ChromeDriver v104.0 level.
- Chrome Browser is updated to current chrome=104.0 (as per chromedriver=104.0 release notes).
As an alternative solution, you can use API, such as Google Maps Place Results API from SerpApi (paid API with a free plan that handles blocks and parsing on their backend).
To begin with, we create a list of the necessary addresses and a list with coordinates to them (the coordinates can be found in the URL of the address we need):
# addresses from which we want to extract the name of the cities
addresses = [
'Bälliz 22, Switzerland',
'Blümlisalpstrasse 36, Switzerland',
'Stauffacherstrasse 105, Switzerland',
'Am Wasser 3, Switzerland',
'Ringstrasse 18, Switzerland'
]
# GPS coordinates of location where you want your q (query) to be applied
# those coordinates are taken from the Google Maps URL
geo_coordinates = [
'@46.7600484,7.6155472,14.08z',
'@46.7810488,7.574276,14z',
'@47.3792421,8.5218228,16z',
'@47.4039247,8.5970111,16z',
'@47.4139972,9.190949,13z'
]
Next, we loop through the lists using zip()
, get the full address from API response and use regular expression to extract the name of the city:
city_name = re.search(r'd+s(w+),', results['place_results']['address']).group(1)
Check full code in online IDE.
from serpapi import GoogleSearch
import json, re, os
# addresses from which we want to extract the name of the cities
addresses = [
'Bälliz 22, Switzerland',
'Blümlisalpstrasse 36, Switzerland',
'Stauffacherstrasse 105, Switzerland',
'Am Wasser 3, Switzerland',
'Ringstrasse 18, Switzerland'
]
# GPS coordinates of location where you want your q (query) to be applied
geo_coordinates = [
'@46.7600484,7.6155472,14.08z',
'@46.7810488,7.574276,14z',
'@47.3792421,8.5218228,16z',
'@47.4039247,8.5970111,16z',
'@47.4139972,9.190949,13z'
]
for address, coordinates in zip(addresses, geo_coordinates):
params = {
"api_key": "...", # serpapi key, https://serpapi.com/manage-api-key
"engine": "google_maps", # SerpApi search engine
"type": "search", # list of results for the query
"google_domain": "google.com", # google domain
"q": address, # query
"hl": "en", # language
"ll": coordinates # GPS coordinates
}
search = GoogleSearch(params) # where data extraction happens on the backend
results = search.get_dict() # JSON -> Python dict
city_name = re.search(r'd+s(w+),', results['place_results']['address']).group(1)
print(f'City name: {city_name}')
Example output:
City name: Thun
City name: Heimberg
City name: Zürich
City name: Dübendorf
City name: Neuenhof
There’s a Using Google Maps Place Results API from SerpApi using Python blog post if you need a little bit more code explanation and how to extract other data.
Disclaimer, I work for SerpApi.