无法点击第一个建议或第一个值

时间:2018-06-25 10:19:14

标签: python selenium selenium-chromedriver

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

  

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

2 个答案:

答案 0 :(得分:0)

在发送第一个建议旁边的字符序列 La Rochelle 之后,您必须诱使 WebDriverWait 使元素可点击您可以使用以下解决方案:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_argument('disable-infobars')
    driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("http://www.lacoteimmo.com/prix-de-l-immo/location/pays/france.htm#/")
    driver.find_element_by_css_selector("#mapAutosuggest").send_keys("La Rochelle") # Enter city
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.slam-aui-results>span.slam-aui-results-line.focus"))).click()
    
  • 浏览器快照:

La Rochelle

答案 1 :(得分:0)

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