Aditya Aditya - 6 months ago 27
Python Question

Selenium handling pop-up&Google Autocomplete

I am trying to do web scraping from http://www.gps-coordinates.net/ using selenium, and I have problem in 2 areas.


  1. Google Autocomplete causes me to unable to click the 'get
    coordinates' button. I tried to solve it but sometimes it still
    doesnt work. ( Message: Element is not clickable at point (280, 17.800003051757812). Other element would receive the click )

  2. Pop up indicates that there are no result available, but I still fail to handle the pop-up.



`

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
import openpyxl
import xlsxwriter
import pyperclip

driver = ""

def setup():
global driver
driver = webdriver.Firefox()
driver.get("http://www.gps-coordinates.net/")
assert 'Google Map' in driver.title

def sele(address):
setup()
global driver
query = driver.find_element_by_id('address')
query.clear()
query.send_keys(address)
query.send_keys(Keys.RETURN)
driver.implicitly_wait(0.7)
query.send_keys(Keys.DOWN) #To solve in case there is autocomplete by Google
query.send_keys(Keys.RETURN)
button = driver.find_element_by_xpath("//*[@id='wrap']/div[2]/div[4]/div[1]/form[1]/div[2]/div/button")
button.click()
driver.implicitly_wait(0.7)
if EC.alert_is_present(): #If there is a popup, that means there is no result for the geocoding
alert=driver.switch_to_alert()
alert.accept()
latlong = ['Fail','Fail']
return latlong
else:
latraw = driver.find_element_by_id('latitude')
longraw = river.find_element_by_id('longitude')
latraw.send_keys(Keys.CONTROL,'A')
latraw.send_keys(Keys.CONTROL,'C')
lat = pyperclip.paste()
latraw.clear()
longraw.send_keys(Keys.CONTROL,'A')
longraw.send_keys(Keys.CONTROL,'C')
long = pyperclip.paste()
latraw.clear()
return [lat,long]

Answer

I solved my problem with this code:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
import openpyxl
import xlsxwriter
import pyperclip

driver = ""

def setup():
    global driver
    driver = webdriver.Firefox()
    driver.get("http://www.gps-coordinates.net/")
    assert 'Google Map' in driver.title

def sele(address):
    global driver
    query = driver.find_element_by_id('address')
    query.clear()
    query.send_keys(address)
    query.send_keys(Keys.RETURN)
    driver.implicitly_wait(0.7)
    query.send_keys(Keys.DOWN) #To solve in case there is autocomplete by Google
    query.send_keys(Keys.RETURN)
    button = driver.find_element_by_xpath("//*[@id='wrap']/div[2]/div[4]/div[1]/form[1]/div[2]/div/button")
    button.click()
    driver.implicitly_wait(0.7)
    try:
        alert=driver.switch_to_alert()
        alert.accept()
        latlong = ['Fail','Fail']
        return latlong
    except Exception :
        latraw = driver.find_element_by_id('latitude')
        longraw = driver.find_element_by_id('longitude')
        newquery = driver.find_element_by_id('address')
        lat = latraw.get_attribute('value')
        long = longraw.get_attribute('value')
        query = newquery.get_attribute('value')
        return [lat,long,query]

def wrapper(inputad,outputad,k):
    InFile = openpyxl.load_workbook(inputad)
    Sheet = InFile['Sheet1']
    workbook = xlsxwriter.Workbook(outputad)
    worksheet = workbook.add_worksheet()
    TotalLength = Sheet.max_row
    ProgressChecker = 0

    for i in range(1, TotalLength +1):
        ProgressChecker = ProgressChecker + 1
        addtext = Sheet[i][k].value
        try:
            latlong = sele(addtext)
            worksheet.write(i,0,addtext)
            worksheet.write(i,1,latlong[0])
            worksheet.write(i,2,latlong[1])
            worksheet.write(i,2,latlong[2])
            print("Progress: ", ProgressChecker, " out of " ,TotalLength)
        except Exception :
            worksheet.write(i,0,addtext)
            worksheet.write(i,1,"Failure Inside Iteration")
            print("Progress: ", ProgressChecker, " out of " ,TotalLength)
            continue
    complete = "complete"
    return complete