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

Nike登录Selenium脚本遇渲染超时问题求解决方案

问题

我编写了一段用于Nike德国站登录及后续操作的Python Selenium脚本,目前核心问题是点击Men按钮后触发超时错误,日志提示timeout: 接收渲染器消息超时:2.000,无法继续执行登录流程。

核心脚本代码

try:
    LOGGER.info("Requesting page: " + NIKE_URL)
    driver.get(NIKE_URL)
except TimeoutException:
    LOGGER.info("Page load timed out but continuing anyway")

# 切换到Men标签以触发登录按钮显示
LOGGER.info("Waiting for Men button to become clickable")
wait_until_clickable(driver=driver, xpath="//a[@class='pre-desktop-menu-link headline-5 prl3-lg pt4-sm d-sm-b no-outline']", duration=10)

LOGGER.info("Clicking men button")
driver.find_element("xpath", "//a[@class='pre-desktop-menu-link headline-5 prl3-lg pt4-sm d-sm-b no-outline']").click()

LOGGER.info("Waiting for login button to become clickable")
wait_until_clickable(driver=driver, xpath="//button[@class='nav-btn p0-sm d-sm-b body-4 u-bold ml2-sm mr2-sm']", duration=10)

LOGGER.info("Clicking login button")
driver.find_element("xpath", "//button[@class='nav-btn p0-sm d-sm-b body-4 u-bold ml2-sm mr2-sm']").click()

LOGGER.info("Waiting for login fields to become visible")
wait_until_visible(driver=driver, xpath="//input[@name='emailAddress']")

LOGGER.info("Entering username and password")
email_input = driver.find_element_by_xpath("//input[@name='emailAddress']")
email_input.clear()
email_input.send_keys(username)
password_input = driver.find_element_by_xpath("//input[@name='password']")
password_input.clear()
password_input.send_keys(password)

LOGGER.info("Logging in")
driver.find_element_by_xpath("//input[@value='ANMELDEN']").click()
wait_until_visible(driver=driver, xpath="//span[text()='Mein Konto']")

LOGGER.info("Successfully logged in")

def wait_until_clickable(driver, xpath=None, class_name=None, duration=10000, frequency=0.01):
    if xpath:
        WebDriverWait(driver, duration, frequency).until(EC.element_to_be_clickable((By.XPATH, xpath)))
    elif class_name:
        WebDriverWait(driver, duration, frequency).until(EC.element_to_be_clickable((By.CLASS_NAME, class_name)))

def wait_until_visible(driver, xpath=None, class_name=None, duration=10000, frequency=0.01):
    if xpath:
        WebDriverWait(driver, duration, frequency).until(EC.visibility_of_element_located((By.XPATH, xpath)))
    elif class_name:
        WebDriverWait(driver, duration, frequency).until(EC.visibility_of_element_located((By.CLASS_NAME, class_name)))

# 其他辅助函数及main方法省略

执行日志

2022-09-14 21:27:35,570 [PID 5096] [Thread 3292] [INFO] [root] 请求页面:https://www.nike.com/de/de_de
2022-09-14 21:27:37,578 [PID 5096] [Thread 3292] [INFO] [root] 页面加载超时,但仍继续执行
2022-09-14 21:27:37,579 [PID 5096] [Thread 3292] [INFO] [root] 等待Men按钮可点击
2022-09-14 21:27:37,763 [PID 5096] [Thread 3292] [INFO] [root] 点击Men按钮
2022-09-14 21:27:39,868 [PID 5096] [Thread 3292] [ERROR] [root] 登录失败:消息:timeout: 接收渲染器消息超时:2.000

解决思路与方案

1. 调整页面加载超时配置

当前默认page_load_timeout仅2秒,远不足以完成Nike页面的渲染加载,修改为15-20秒:

parser.add_argument("--page_load_timeout", type=int, default=15)

同时在driver.get(NIKE_URL)后添加页面就绪等待:

WebDriverWait(driver, 15).until(lambda d: d.execute_script('return document.readyState') == 'complete')

2. 修复等待函数参数错误

等待函数中duration参数单位为秒,但默认值设为10000(相当于2.7小时),且检测频率0.01秒过于频繁,调整为合理值:

def wait_until_clickable(driver, xpath=None, class_name=None, duration=10, frequency=0.5):
def wait_until_visible(driver, xpath=None, class_name=None, duration=10, frequency=0.5):

3. 优化元素定位策略

Nike页面class属性会动态变化,改用更稳定的文本/属性定位(注意德国站为德语,Men对应Herren):

  • Men按钮://a[contains(text(),'Herren')]
  • 登录按钮://button[contains(text(),'Anmelden')]

4. 添加反检测浏览器配置

Nike有反爬机制,headless模式易被识别,添加ChromeOptions配置模拟真实用户:

options.add_argument("--disable-blink-features=AutomationControlled")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36")

5. 处理点击后的页面跳转延迟

点击Men按钮后页面会刷新,添加URL变化等待确认页面加载完成:

current_url = driver.current_url
driver.find_element(By.XPATH, "//a[contains(text(),'Herren')]").click()
WebDriverWait(driver, 10).until(EC.url_changes(current_url))

6. 替换过时API

脚本中find_element_by_xpath为Selenium 4.x过时方法,替换为标准写法:

from selenium.webdriver.common.by import By

email_input = driver.find_element(By.XPATH, "//input[@name='emailAddress']")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 06:45:39