2016-05-25 64 views
0

我試圖做從http://www.gps-coordinates.net/使用硒刮網絡,我有2個方面的問題。硒處理彈出和谷歌自動完成

  1. Google自動完成使我無法點擊'獲取 座標'按鈕。我試圖解決它,但有時它仍然 不起作用。 (消息:元素在點(280,17.800003051757812)不可點擊,其他元素會收到點擊)
  2. 彈出表示沒有可用的結果,但仍無法處理彈出窗口。

`

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] 

回答

1

我解決我的問題與此代碼:

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