python - 无法点击第一个建议或第一个值
问题描述
我正在尝试从网站上抓取数据
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()
解决方案
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()
推荐阅读
- graph - 如何在graphviz点节点的TABLE标签中左对齐多行文本文字?
- python - 我想将所有条目导出到 excel 文件
- spring-boot - 在springboot中验证日期时间格式“yyyy-MM-dd HH:mm:ss”
- javascript - 在 swal 之前验证必填字段?
- owl - 如何从 HermiT 输出中确定问题在本体中的位置
- linux - 超过 7 天的 Gzip 或 tars 日志
- firebase - Firebase 函数后端错误:无法导入库
- python-3.x - Auth0 登录 URL 404 未找到
- behat - 如何修复 Behat 3 / Mink 中的浏览器窗口大小?
- angular - 如何在 ckeditor4-angular 中添加 MathType 插件以使用数学公式