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

使用Python Selenium访问EDF登录页点击后显示空白的问题求助

问题描述

尝试通过Python Selenium打开法国电力公司(EDF)的登录页面以下载能源数据,执行代码点击“Tout Accepter”按钮后页面显示空白,但用常规浏览器(含无痕模式)访问可正常跳转至登录页。Chrome和Firefox驱动测试结果一致,怀疑是重定向问题但无法理清原因。

使用的代码如下:

import time
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

# Step 1: Start Selenium WebDriver
driver = webdriver.Firefox()  # Ensure options are passed correctly
driver.get("https://particulier.edf.fr/bin/edf_rc/servlets/sasServlet?processus=TDB")  # Replace with the actual login page URL

# Step 2a: Wait for the "Tout Accepter" button and click it
accept_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "popin_tc_privacy_button_3")))
accept_button.click()

time.sleep(5.0)
driver.quit()

解决思路及方案

1. 模拟真实浏览器环境,规避自动化检测

网站常通过检测Selenium的特征(如默认用户代理、自动化标记)阻止正常跳转,添加浏览器配置消除这些差异:

Firefox配置示例

import time
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

options = Options()
# 禁用自动化控制标记
options.add_argument("--disable-blink-features=AutomationControlled")
# 设置真实用户代理
options.set_preference("general.useragent.override", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0")
# 确保JavaScript启用
options.set_preference("javascript.enabled", True)

driver = webdriver.Firefox(options=options)
driver.get("https://particulier.edf.fr/bin/edf_rc/servlets/sasServlet?processus=TDB")

accept_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "popin_tc_privacy_button_3")))
accept_button.click()

# 等待重定向完成
WebDriverWait(driver, 15).until(EC.url_contains("login"))
driver.quit()

Chrome配置示例

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36")
options.add_argument("--enable-javascript")
options.add_argument("--disable-extensions")

driver = webdriver.Chrome(options=options)
driver.get("https://particulier.edf.fr/bin/edf_rc/servlets/sasServlet?processus=TDB")

accept_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "popin_tc_privacy_button_3")))
accept_button.click()

WebDriverWait(driver, 15).until(EC.url_contains("login"))
driver.quit()

2. 用JavaScript触发按钮点击

部分网站的按钮点击事件依赖JS绑定,直接模拟点击可能无法触发完整逻辑,改用JS执行点击:

accept_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "popin_tc_privacy_button_3")))
driver.execute_script("arguments[0].click();", accept_button)

3. 显式等待重定向完成,替代固定延迟

time.sleep()无法保证重定向完成,改用显式等待检测URL或标题变化:

# 等待URL包含登录页特征关键字
WebDriverWait(driver, 15).until(EC.url_contains("login"))
# 或等待页面标题符合预期
# WebDriverWait(driver, 15).until(EC.title_contains("EDF - Connexion"))

4. 检查Cookie状态

同意隐私政策后网站会设置会话Cookie,可打印Cookie确认是否正常保存:

accept_button.click()
print(driver.get_cookies())

若缺少必要Cookie,可手动添加或检查浏览器配置是否阻止了Cookie存储。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 13:13:17