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

使用Selenium做freesound.org自动化时点击登录按钮跳转回首页问题

问题原因及修复方案

核心问题点

  • 你调用了submit()方法提交表单,freesound对登录操作的前端校验逻辑绑定在提交按钮的点击事件上,直接调用submit()会跳过该校验逻辑,导致提交的登录请求缺少必要参数,后端校验失败后自动跳转回首页
  • 固定等待time.sleep(1)时长过短,登录页未完全加载就执行元素查找、输入操作,容易出现输入内容丢失、提交异常的问题
  • 所用find_element_by_*是Selenium 3及更早版本的废弃语法,Selenium 4+环境下运行会出现兼容性错误,导致操作未正确执行
  • 登录提交后没有等待跳转逻辑,代码直接结束运行,无法捕获登录后的状态

修复后可运行代码

import os
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 getpass

username = input("请输入用户名: ")
password = getpass.getpass(prompt="请输入密码: ")
os.environ['PATH'] += r"C:/webdriver"
# 可选:添加配置隐藏自动化特征,避免被站点反爬拦截
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options)
driver.maximize_window()
driver.get("https://freesound.org/search/?q=&f=created:[NOW-7DAY%20TO%20NOW]&s=num_downloads+desc&g=0")

# 显式等待登录按钮可点击后再操作
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Log In"))).click()

# 等待用户名输入框加载完成后输入内容
username_input = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "id_username")))
username_input.send_keys(username)

# 等待密码输入框加载完成后输入内容
password_input = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "id_password")))
password_input.send_keys(password)

# 点击提交按钮触发完整登录逻辑,不要调用submit()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input[type="submit"]'))).click()

# 等待跳转完成验证登录状态
time.sleep(5)
print("当前页面标题:", driver.title)

额外注意事项

  • 如果触发了站点的人机验证,需要手动完成验证后再继续后续自动化操作
  • 不要高频重复提交登录请求,避免账号被临时限制登录

内容的提问来源于stack exchange,提问作者Zubair Khan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 13:57:03