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

使用Selenium结合Python实现欧盟FSF网页账号密码自动登录的技术求助

Selenium结合Python实现欧盟FSF网页账号密码自动登录的技术求助

兄弟,我来帮你搞定这个自动登录的问题!先理清楚你的场景和代码里的小问题,再一步步解决登录环节的两个需求。

首先,先还原你的问题场景:

我正在尝试用Python结合Selenium访问欧盟FSF的文件页面:https://webgate.ec.europa.eu/fsd/fsf#!/files,目前已经写出了部分代码,但打开页面后遇到了登录环节的问题:

我的代码如下:

import pandas as pd
import numpy as np
from datetime import date
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
import time
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

if True:
    chromedriver_path = r"./driver/chromedriver"
    browser = webdriver.Chrome(executable_path=chromedriver_path)
    url = "https://webgate.ec.europa.eu/fsd/fsf#!/files"
    browser.get(url)
    escolhe = browser.find_element("xpath", topics_xpath)  # 这里的topics_xpath变量没定义哦
    time.sleep(10)
    escolhe.click()
    time.sleep(60)
    browser.close()

现在页面打开后会先提示我输入用户ID(也就是我的邮箱地址),之后还要输入密码并点击下一步,我想请教两个问题:

  1. 如何用Python自动输入用户ID并点击“Next”按钮?
  2. 如何用Python自动输入密码并点击“Next”按钮?

先提个小建议:清理冗余代码+替换固定sleep

你的代码里有重复导入的模块(比如重复导入了expected_conditions、By、WebDriverWait),可以删掉重复的;另外尽量不要用time.sleep(),网络波动时很容易因为元素没加载完报错,改用**显式等待(WebDriverWait)**更可靠。


问题1:自动输入用户ID并点击Next

首先你需要用浏览器开发者工具(按F12)定位用户ID输入框的真实属性(比如id、name或者xpath),我这里举个常见的例子,你要根据实际页面调整:

修改后的代码片段:

# 先清理重复导入,保留必要模块
import pandas as pd
import numpy as np
from datetime import date
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver

if True:
    chromedriver_path = r"./driver/chromedriver"
    browser = webdriver.Chrome(executable_path=chromedriver_path)
    url = "https://webgate.ec.europa.eu/fsd/fsf#!/files"
    browser.get(url)
    
    # 初始化等待器,最多等待20秒(可根据实际调整)
    wait = WebDriverWait(browser, 20)
    
    # 等待用户ID输入框加载完成(这里假设输入框的id是"username",你要换成实际页面的属性)
    username_input = wait.until(EC.presence_of_element_located((By.ID, "username")))
    # 输入你的邮箱(用户ID)
    username_input.send_keys("你的邮箱地址@xxx.com")
    
    # 等待Next按钮可点击并点击(假设按钮的id是"next",同样要换成实际的)
    next_btn = wait.until(EC.element_to_be_clickable((By.ID, "next")))
    next_btn.click()

问题2:自动输入密码并点击Next

完成第一步后,页面会跳转到密码输入界面,同样用显式等待定位密码框和Next按钮,接上面的代码继续写:

# 等待密码输入框加载完成(假设密码框的id是"password")
password_input = wait.until(EC.presence_of_element_located((By.ID, "password")))
# 输入你的密码
password_input.send_keys("你的登录密码")

# 等待密码环节的Next按钮可点击并点击(注意按钮id可能和之前不同,要实际查看)
password_next_btn = wait.until(EC.element_to_be_clickable((By.ID, "password-next")))
password_next_btn.click()

# 如果后续还有验证步骤(比如短信验证码),再根据页面情况补充代码
# 完成操作后关闭浏览器
# browser.close()

额外注意事项

  • 元素定位要准确:一定要自己用F12查看页面元素的真实属性,我上面写的id都是假设值,实际页面可能用name、xpath或者其他属性,比如如果找不到id,可以用xpath:(By.XPATH, "//input[@placeholder='请输入邮箱']")
  • iframe问题:如果登录框在iframe里,需要先切换到iframe:browser.switch_to.frame("iframe的id或name"),操作完再切回来:browser.switch_to.default_content()
  • 原代码的bug:你的代码里topics_xpath变量没有定义,这会直接报错,要么补充定义这个xpath,要么如果这步是不需要的就删掉

备注:内容来源于stack exchange,提问作者Giampaolo Levorato

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.22 11:14:43