1

我正在尝试从网站 https://www.airdna.co抓取数据

我想获得我管理以下代码的第一个建议的价值;问题是我不能点击第一个城市来获取信息有人可以提出解决这个问题的建议吗

#!/usr/bin/python
# -*- coding: utf-8 -*-
import re
import time
import csv
import unittest
import sys
import datetime
import os.path
import pandas as pd


from datetime import datetime
from selenium import webdriver
from bs4 import NavigableString
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions
from selenium.common.exceptions import WebDriverException
from bs4 import BeautifulSoup
from bs4.element import Tag
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.remote.errorhandler import ErrorHandler
from unidecode import unidecode
import unicodecsv


class MyTestCase():
def setUp(self):
self.driver = webdriver.Chrome()
#self.driver.error_handler = MyHandler()

def main(self):
REGION=[]
INSEE=[] #la liste des départements
CITIES=[]
with open('3000Commun_France.csv') as csvfile:
csv_reader = csv.reader(csvfile)
next(csv_reader) # supression des entêtes
for row in csv_reader:
REGION.append(row[0])
INSEE.append(row[1])
CITIES.append(row[2])
self.driver = webdriver.Chrome()
driver=self.driver
for insee,city in zip(INSEE,CITIES):
print str(city) +" , "+str(insee)
try:
driver.get("https://www.airdna.co/")
driver.implicitly_wait(20)
driver.find_element_by_css_selector("#searchbox_home").send_keys(city+",FR") # Enter city
# Wait until autosuggestion come and click on first suggestion
condition = EC.visibility_of_element_located((By.CSS_SELECTOR, '#searchbox_home + ul > li:nth-child(1)'))
time.sleep(3)
WebDriverWait(driver, 5).until(condition).click()
page = driver.page_source
soup = BeautifulSoup(page, "lxml")
except NavigableString: 
pass
if __name__ == "__main__":
sys.tracebacklimit = 0
MyTestCase().main()

在此处输入图像描述

4

2 回答 2

2

根据您to get the value of the first suggestion在网站内的问题,https://www.airdna.co/一旦您发送搜索相关的字符序列,您需要诱导WebDriverWait以使所需元素可见/可点击,您可以使用以下解决方案:

  • 代码块:

    driver.get("https://www.airdna.co/")
    WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR,"input.ui-autocomplete-input"))).send_keys("la roch")
    print(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"ul.ui-menu.ui-widget.ui-widget-content.ui-autocomplete.ui-front>li>div"))).get_attribute("innerHTML"))
    
  • 控制台输出:

    La Rochelle, FR
    
  • 浏览器快照:

第一建议


如果您想单击可以使用的第一个自动建议:

  • 代码块:

    driver.get("https://www.airdna.co/")
    WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR,"input.ui-autocomplete-input"))).send_keys("la roch")
    print(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"ul.ui-menu.ui-widget.ui-widget-content.ui-autocomplete.ui-front>li>div"))).get_attribute("innerHTML"))
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"ul.ui-menu.ui-widget.ui-widget-content.ui-autocomplete.ui-front>li>div"))).click()
    
  • 控制台输出:

    La Rochelle, FR
    
  • 浏览器快照:

click_first_suggestion

于 2018-07-11T12:33:21.230 回答
0

用动作类替换点击事件:

from selenium.webdriver.common.action_chains import ActionChains

actions = ActionChains(driver)
actions.move_to_element("Your Web Element").click().perform()
于 2018-07-11T12:18:00.637 回答