简体   繁体   中英

How to click on the first auto suggestion or first searched value on 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()

在此处输入图片说明

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:

click_first_suggestion

Replace click event with action class:

from selenium.webdriver.common.action_chains import ActionChains

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM