You need to enable JavaScript to run this app. 使用Python Selenium定位wikiloc网站下载按钮失败求助
优惠活动
大模型
产品
解决方案
定价
更多

使用Python Selenium定位wikiloc网站下载按钮失败求助

问题修复方案

核心问题优化点

  1. 登录状态验证优化
    不要使用固定时长等待,改用显式等待判断登录是否完成,避免页面未加载完就跳转导致登录失效。高版本Selenium已弃用旧的find_element_by_*系列方法,统一使用By类指定定位方式。
  2. 元素定位可靠性优化
    绝对XPath容错性极低,页面结构稍有变动就会失效,改用相对定位、属性选择器的方式,同时给所有点击操作加显式等待,确保元素加载完成且可点击后再执行操作。
  3. Colab环境适配
    Colab运行Selenium默认是无头模式,容易被网站反爬策略识别,需要添加UA伪装、开启cookie存储,避免登录状态丢失。

修正后可运行代码

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
import time
import pandas as pd

# Selenium配置 适配Colab环境
options = webdriver.ChromeOptions()
options.add_argument('--headless=new')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
# 伪装浏览器标识避免被反爬识别
options.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36')
# 持久化存储Cookie 保留登录状态
options.add_argument('--user-data-dir=./chrome_profile')

driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver, 15)

# 登录逻辑
driver.get('https://www.wikiloc.com/wikiloc/start.do')
wait.until(EC.presence_of_element_located((By.NAME, 'email'))).send_keys('testacount852')
wait.until(EC.presence_of_element_located((By.NAME, 'password'))).send_keys('49Vchu4njDrrT7.')
wait.until(EC.element_to_be_clickable((By.ID, 'submit-button'))).click()

# 校验登录是否成功 检测登录后才会出现的用户头像元素
try:
    wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'user-menu-avatar')))
except:
    print("登录失败,请检查账号密码或是否触发了人机验证")
    driver.quit()
    exit()

# 爬取轨迹列表
driver.get("https://www.wikiloc.com/trails/outdoor/france/brittany")
links = []
names = []
for _ in range(4):
    temp = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, 'a.trail-title.dont-break-out')))
    temp_links = [x.get_attribute('href') for x in temp]
    temp_names = [x.text for x in temp]
    links.extend(temp_links)
    names.extend(temp_names)
    try:
        wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'next'))).click()
        time.sleep(2)
    except:
        break

# 批量下载轨迹
df = pd.DataFrame()
for link in links:
    driver.get(link)
    # 点击下载按钮
    wait.until(EC.element_to_be_clickable((By.ID, 'download-button'))).click()
    # 选择下载格式
    wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'active'))).click()
    # 提交下载请求 用属性选择器替代原绝对XPath
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div.download-form input[type="submit"]'))).click()
    time.sleep(4)

df['Names'] = pd.Series(names)
df['Links'] = pd.Series(links)
df.to_csv('test1.csv')
driver.close()

注意事项

  • 控制爬取频率,避免频繁请求触发网站反爬封禁IP
  • 若网站弹出人机验证,可接入打码服务或者手动完成验证后再继续爬取
  • 下载的轨迹文件默认保存在Colab当前运行目录,可通过左侧文件面板查看导出

内容的提问来源于stack exchange,提问作者Ahmad Khalil

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.21 13:06:04