如何在https://www.airdna.co/上单击第一个自动建议或第一个搜索的值

时间:2018-07-11 12:02:34

标签: python selenium selenium-webdriver selenium-chromedriver webdriverwait

我正在尝试从网站上抓取数据 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()

enter image description here

2 个答案:

答案 0 :(得分: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
    
  • 浏览器快照:

first_suggestion


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

  • 代码块:

    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

答案 1 :(得分:0)

用操作类替换点击事件:

from selenium.webdriver.common.action_chains import ActionChains

actions = ActionChains(driver)
actions.move_to_element("Your Web Element").click().perform()