首页 > 解决方案 > 无法点击第一个建议或第一个值

问题描述

我正在尝试从网站上抓取数据

http://www.lacoteimmo.com/prix-de-l-immo/location/pays/france.htm#/

我正在努力点击第一个建议。我管理此代码如下:

#!/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 geopy.geocoders import GoogleV3
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

def main(self):
CITIES = ["La Rochelle"]
self.driver = webdriver.Chrome()
driver=self.driver
ID=1
for city in CITIES:
print str(city) +" , "+str(ID)
ID+=1
try:
driver.get("http://www.lacoteimmo.com/prix-de-l-immo/location/pays/france.htm#/")
driver.implicitly_wait(30) 
driver.find_element_by_css_selector("#mapAutosuggest").send_keys(city) # Enter city
# Wait until autosuggestion come and click on first suggestion
condition = EC.visibility_of_element_located((By.CSS_SELECTOR, '#mapAutosuggest + span > span:nth-child(1)'))
WebDriverWait(driver, 5).until(condition).click()
driver.implicitly_wait(50) 
except NoSuchElementException: #spelling error making this code not work as expected
pass
self.driver.quit()

标签: pythonseleniumselenium-chromedriver

解决方案


import time
from geopy.geocoders import GoogleV3
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 selenium.webdriver.chrome.options import Options

CITIES = ["La Rochelle"]
chrome_options = Options()
chrome_options.add_argument("--start-maximized")
# chrome_options.add_argument("--headless")

driver = webdriver.Chrome(
    executable_path='chrome_driver_path'
    , chrome_options=chrome_options
)

ID=1
for city in CITIES:
    print(str(city) + " , " +str(ID))
    ID+=1

try:
    driver.get("http://www.lacoteimmo.com/prix-de-l-immo/location/pays/france.htm#/")
    driver.implicitly_wait(30) 
    driver.find_element_by_css_selector("#mapAutosuggest").send_keys(city) # Enter city
    # Wait until autosuggestion come and click on first suggestion
    condition = EC.visibility_of_element_located((By.CSS_SELECTOR, '.slam-aui-results > span:nth-child(1)'))
    WebDriverWait(driver, 5).until(condition)
    firstResult = driver.find_element_by_css_selector(".slam-aui-results > span:nth-child(1)")
    firstResult.click()
    time.sleep(10)
    driver.implicitly_wait(50) 
except NoSuchElementException: #spelling error making this code not work as expected
    pass
driver.quit()

推荐阅读