python - Selenium handling pop-up&Google Autocomplete -
i trying web scraping http://www.gps-coordinates.net/ using selenium, , have problem in 2 areas.
- google autocomplete causes me unable click 'get coordinates' button. tried solve still doesnt work. ( message: element not clickable @ point (280, 17.800003051757812). other element receive click )
- pop indicates there no result available, still fail handle pop-up.
`
from selenium import webdriver selenium.webdriver.support.ui import webdriverwait selenium.webdriver.common.keys import keys selenium.webdriver.support import expected_conditions 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 autocomplete 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 popup, means there no result 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]
i solved problem code:
from selenium import webdriver selenium.webdriver.support.ui import webdriverwait selenium.webdriver.common.keys import keys selenium.webdriver.support import expected_conditions 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 autocomplete 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 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
Comments
Post a Comment