You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:解决Selenium无法点击Wunderground历史天气页View按钮问题

问题:Selenium无法点击Wunderground历史页面的「View」按钮

我用Python的Selenium访问https://www.wunderground.com/history时,始终无法触发页面上的「View」按钮(ID为dateSubmit)。

已尝试的无效方法

  • 使用ActionChains:actions.move_to_element(button_view).click().perform()
  • 直接定位元素点击:browser.find_element(By.ID, 'dateSubmit').click()
  • 等待元素可点击后操作:wait.until(EC.element_to_be_clickable((By.ID, 'dateSubmit')))
  • 双击元素:actions.move_to_element(button_view).double_click(button_view)

特殊现象

访问子页面(如https://www.wunderground.com/history/daily/KLGA/)时,在historySearch输入框中输入内容后按钮会自动触发,但主/history页面完全无效。

我怀疑问题出在点击按钮前的前置操作:主页面需要点击搜索框、输入地点(比如「New York, NY」)并从自动补全下拉菜单选择选项,但我无法选中下拉菜单的选项,只能尝试让输入框失焦来自动选择第一个补全项,但依然无法解决按钮点击问题。

我的代码

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from datetime import date, timedelta
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from collections import Counter
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from bs4 import BeautifulSoup
from datetime import datetime

import time
import logging
import pandas as pd

def get_weather_data4( year, month, day, browser):
    try:
        # Open browser and navigate to the website
        #browser = webdriver.Chrome()
        wait = WebDriverWait(browser, 20)
        browser.get('https://www.wunderground.com/history')
        wait.until(EC.presence_of_element_located((By.ID, 'yearSelection')))
        wait.until(EC.presence_of_element_located((By.ID, 'historySearch')))
        
        # Select year, month, and day from the drop-down menus
        actions = ActionChains(browser)
        year_select = Select(browser.find_element(By.ID,'yearSelection'))
        year_select.select_by_value(str((2023-year))+':'+' '+str(year))
        month_select = Select(browser.find_element(By.ID,'monthSelection'))
        month_select.select_by_value(str(month))
        day_select = Select(browser.find_element(By.ID,'daySelection'))
        day_select.select_by_value(str((day-1))+':'+' '+str(day))
     
        search_field = wait.until(EC.visibility_of_element_located((By.ID, 'historySearch')))
        search_field.click()
        search_field.send_keys("New York, NY", Keys.ENTER)
        #search_field.send_keys(Keys.TAB * 2 )#+ Keys.RETURN * 2)
     

        try:
            wait.until(EC.element_to_be_clickable((By.ID, 'dateSubmit')))
            button_view = browser.find_element(By.ID, 'dateSubmit')
            #button_view.send_keys("",Keys.ENTER)
            actions.move_to_element(button_view).double_click(button_view)
        except TimeoutError as te:
            print(f"The following element caused a timeout error: {te}")
       
    # Click the "View" button to load the data for the selected date FAILED ATTEMPTS
        #button_view = wait.until(EC.element_to_be_clickable((By.ID, 'dateSubmit')))
        #actions.move_to_element(button_view).click().perform()
        #button_view.click()
        #browser.find_element(By.ID, 'dateSubmit').click()
        #WebDriverWait(browser, 2)
        #browser.find_element(By.ID, 'dateSubmit').click()
        
        # Wait for the data to load
        wait.until_not(EC.presence_of_element_located((By.XPATH, '//div[@class="loading-indicator"]')))
        wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'row')))
        wait.until(EC.presence_of_element_located((By.XPATH, '//table[@class="mat-table cdk-table mat-sort ng-star-inserted"]')))
    
    except TimeoutError:
        raise Exception("Timeout Error: Could not load data from URL")
    
    # Extract the data from the table using BeautifulSoup
    soup = BeautifulSoup(browser.page_source, 'html.parser')
    table = soup.select_one('table[class*="mat-table cdk-table mat-sort ng-star-inserted"]')
    rows = table.find_all('tr',{'class':'mat-row cdk-row ng-star-inserted'})
  # Code continues... 

环境信息

  • Selenium版本:4.7.2
  • 浏览器:Firefox
  • 操作系统:Windows 10

解决建议

  1. 正确处理自动补全下拉菜单:主页面的搜索框必须完成地点选择才会激活「View」按钮,直接回车或失焦可能没有触发页面的地点确认逻辑。可以等待自动补全选项出现后,通过索引或文本定位并点击:
# 输入地点后等待下拉选项加载
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'ul[role="listbox"] li')))
# 选择第一个选项
first_option = browser.find_element(By.CSS_SELECTOR, 'ul[role="listbox"] li')
first_option.click()
  1. 使用JavaScript强制点击:如果按钮存在前端事件绑定问题,常规点击方法无效,可以用JS绕过:
button_view = wait.until(EC.presence_of_element_located((By.ID, 'dateSubmit')))
# 先确认按钮未被禁用
if not button_view.get_attribute('disabled'):
    browser.execute_script("arguments[0].click();", button_view)
  1. 调整日期选择方式:页面的日期下拉框是自定义Angular组件,而非原生<select>,用Select类操作可能无法触发页面的日期确认事件,改为模拟人工点击选择:
# 示例:选择年份
year_dropdown = wait.until(EC.element_to_be_clickable((By.ID, 'yearSelection')))
year_dropdown.click()
year_option = wait.until(EC.element_to_be_clickable((By.XPATH, f'//span[text()="{year}"]')))
year_option.click()
  1. 增加操作间隔等待:页面存在异步加载逻辑,在输入地点、选择日期后,等待1-2秒再尝试点击按钮,确保页面状态更新完成。

内容的提问来源于stack exchange,提问作者Shai Shillo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 19:35:46