如何在https://www.airdna.co/上单击第一个自动建议或第一个搜索的值 [英] How to click on the first auto suggestion or first searched value on https://www.airdna.co/

查看:96
本文介绍了如何在https://www.airdna.co/上单击第一个自动建议或第一个搜索的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to scrape the data from the website https://www.airdna.co

我想获得第一个建议的价值 我管理以下代码; 问题是我无法单击第一个城市来获取信息,有人可以提出解决此问题的建议

i want to get the value of the first suggestion i managed the following code; The problem was i can't click on the first city to get the information can someone has a suggestion to solve this probleme

#!/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()

推荐答案

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

As per your question to get the value of the first suggestion within the website https://www.airdna.co/ once you send the search related character sequence you need to induce WebDriverWait for the desired element to be visible/clickable and you can use the following solution:

  • 代码块:

  • Code Block:

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"))

  • 控制台输出:

  • Console Output:

    La Rochelle, FR
    

  • 浏览器快照:

  • Browser Snapshot:

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

    Incase you want to click on the first auto suggestion you can use:

    • 代码块:

    • Code Block:

    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()
    

  • 控制台输出:

  • Console Output:

    La Rochelle, FR
    

  • 浏览器快照:

  • Browser Snapshot:

    这篇关于如何在https://www.airdna.co/上单击第一个自动建议或第一个搜索的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆