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

Selenium使用time.sleep()和WebDriverWait仍无法加载页面求助

Selenium加载CQVIP页面空白的解决建议

核心排查方向:反爬拦截与浏览器配置优化

目标网站大概率启用了反爬机制,识别出自动化浏览器后返回空白页面。以下是针对性的代码修改方案:

1. 启用无痕模式并添加防检测参数

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

options = Options()
# 启用无痕模式
options.add_argument("--incognito")
# 禁用自动化提示
options.add_argument("--disable-blink-features=AutomationControlled")
# 隐藏webdriver标识
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)

driver = webdriver.Chrome(options=options)
# 执行JS移除webdriver属性
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")

driver.get("http://qikan.cqvip.com/Qikan/Article/Detail?id=35677542")

# 等待页面核心元素加载(示例为文章标题类,可根据实际页面调整)
try:
    WebDriverWait(driver, 20).until(
        EC.presence_of_element_located((By.CLASS_NAME, "article-title"))
    )
    print(driver.page_source)
except Exception as e:
    print(f"等待超时或元素未找到: {e}")
finally:
    driver.quit()

2. 模拟真实浏览器请求头

在ChromeOptions中添加匹配真实用户的UA参数:

options.add_argument("user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36")
# 可选:禁用图片加载加快速度
options.add_argument("--blink-settings=imagesEnabled=false")

3. 用CDP协议强化反检测能力

通过Chrome DevTools协议在页面加载前注入脚本,隐藏更多自动化特征:

driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
    "source": """
        Object.defineProperty(navigator, 'webdriver', {get: () => undefined});
        Object.defineProperty(navigator, 'plugins', {get: () => [1, 2, 3, 4, 5]});
        Object.defineProperty(navigator, 'languages', {get: () => ['zh-CN', 'zh']});
    """
})

4. 排查网络环境限制

若存在地区或代理限制,可添加代理配置:

options.add_argument("--proxy-server=http://你的代理地址:端口")

额外注意事项

  • 确认Chrome浏览器版本与ChromeDriver完全匹配(当前132版本需对应Chrome浏览器132.x系列)
  • 避免高频请求,添加随机间隔(如time.sleep(random.uniform(2,5)))
  • 可尝试切换Firefox+GeckoDriver测试,排除Chrome专属拦截逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 22:52:09