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

Selenium访问目标URL返回空白页面问题求助

解决Selenium访问页面空白的问题

问题原因

目标网站的反爬机制识别出了Selenium的自动化特征,或你的浏览器配置导致页面未正常渲染。以下是针对性修复方案:

  • 调整页面加载策略
    你设置的page_load_strategy = 'none'会让浏览器跳过页面加载等待,直接执行后续代码,导致页面还未渲染就被判定为加载完成。将其改为'normal'(等待页面完全加载)或'eager'(等待DOM加载完成):

    options.page_load_strategy = 'normal'
    
  • 添加真实用户代理
    Selenium默认用户代理易被反爬系统识别,手动添加一个当前Chrome版本的用户代理:

    options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36")
    
  • 替换固定sleep为智能等待
    固定time.sleep()无法适配不同网络环境的加载速度,改用WebDriverWait等待页面关键元素出现,确保渲染完成:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    # 替换原有固定sleep
    WebDriverWait(driver, 20).until(
        EC.presence_of_element_located((By.TAG_NAME, "h1"))  # 等待页面标题元素加载
    )
    
  • 补充浏览器启动参数
    添加模拟真实浏览器的参数,进一步规避反爬检测:

    options.add_argument("--start-maximized")  # 最大化窗口,模拟真人操作
    options.add_argument("--disable-extensions")  # 禁用扩展
    options.add_argument("--disable-popup-blocking")  # 禁用弹窗拦截
    

修改后的完整代码

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

def scrape_website(url):
    options = Options()
    options.page_load_strategy = 'normal'  # 调整为正常加载策略
    preferences = {"safebrowsing.enabled": "false"}
    options.add_argument("disable-blink-features=AutomationControlled")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    options.add_argument('--no-proxy-server')
    options.add_experimental_option("prefs", preferences)
    # 添加用户代理
    options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36")
    # 补充浏览器参数
    options.add_argument("--start-maximized")
    options.add_argument("--disable-extensions")
    options.add_argument("--disable-popup-blocking")

    driver = webdriver.Chrome(options=options)
    driver.get(url)
    
    # 智能等待页面关键元素加载
    try:
        WebDriverWait(driver, 20).until(
            EC.presence_of_element_located((By.TAG_NAME, "h1"))
        )
        print("Page Title:", driver.title)
        # 可选:等待更多内容加载
        time.sleep(3)
    except Exception as e:
        print("页面加载超时或未找到关键元素:", e)
    finally:
        driver.quit()

url = 'https://www.realestate.com.au/buy/in-canberra+-+greater+region,+act/list-1'
scrape_website(url)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 03:23:35