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

Selenium调用driver.get()加载特定URL时卡顿问题求助

问题解决:Selenium卡在driver.get(url),浏览器加载完成但代码不执行

环境信息

  • Python 3.8.10
  • Selenium 4.0.0a6
  • Chrome版本:120.0.0.0
  • Chromedriver版本:2.41

问题现象

加载Google页面(url2)时代码正常运行并输出complete,但加载香港赛马会六合彩结果页(url1)时,代码卡在driver.get(url1)行,浏览器已显示网页内容,但后续代码完全不执行。

原始代码

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

def url_driver():

    chromedriver_path = 'D:\\Programming\\Program\\Python\\chromedriver_2.41\\chromedriver.exe'
    chrome_binary_path = 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'
    
    service = Service(chromedriver_path)
    options = webdriver.ChromeOptions()
    options.binary_location = chrome_binary_path
    
    options.add_argument("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
    
    driver = webdriver.Chrome(service=service, options=options)

    return driver

if __name__ == '__main__':

    url1 = 'https://bet.hkjc.com/marksix/Results.aspx?lang=ch'
    url2 = 'https://www.google.com'

    driver = url_driver()
    driver.get(url1)
    
    print('complete')

核心原因

  1. Chromedriver与Chrome版本严重不兼容:你用的chromedriver 2.41仅支持Chrome 67-69版本,而当前Chrome是120.0.0.0,版本差距过大直接导致Selenium与浏览器通信异常,出现加载阻塞。
  2. 默认加载策略等待资源全量加载:driver.get()默认会等待页面document.readyState变为complete才继续执行,但目标页面存在大量动态加载的广告、统计脚本等资源,这类资源加载缓慢甚至阻塞,导致Selenium一直处于等待状态。

解决方案

1. 升级Chromedriver到匹配版本

下载与Chrome 120.0.0.0主版本号一致的chromedriver(即120.x.x.x版本),替换代码中的chromedriver_path路径。

2. 修改页面加载策略

将加载策略设置为eager(仅等待DOM加载完成,无需等待图片、视频等非关键资源)或none(不等待任何加载完成),避免非关键资源阻塞代码:

# 在options配置中添加
options.page_load_strategy = 'eager'

3. 增加显式等待(可选)

在driver.get()后添加显式等待,确保页面关键元素加载完成后再执行后续代码,提升稳定性:

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

# driver.get(url1)之后添加
WebDriverWait(driver, 15).until(
    EC.title_contains('六合彩結果')  # 可替换为页面内实际关键元素的定位逻辑
)

4. 禁用不必要资源加载(可选)

通过ChromeOptions禁用图片、非必要JS,加快页面加载速度:

# 禁用图片加载
options.add_argument('--blink-settings=imagesEnabled=false')
# 禁用非必要JS(若页面功能不受影响)
options.add_argument('--disable-javascript')

修改后的完整代码示例

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def url_driver():
    # 替换为匹配Chrome 120的chromedriver路径
    chromedriver_path = 'D:\\Programming\\Program\\Python\\chromedriver_120\\chromedriver.exe'
    chrome_binary_path = 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'
    
    service = Service(chromedriver_path)
    options = webdriver.ChromeOptions()
    options.binary_location = chrome_binary_path
    options.add_argument("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
    # 设置页面加载策略为eager
    options.page_load_strategy = 'eager'
    
    driver = webdriver.Chrome(service=service, options=options)
    return driver

if __name__ == '__main__':
    url1 = 'https://bet.hkjc.com/marksix/Results.aspx?lang=ch'
    driver = url_driver()
    driver.get(url1)
    
    # 等待页面关键元素加载
    WebDriverWait(driver, 15).until(
        EC.title_contains('六合彩結果')
    )
    print('complete')
    driver.quit()  # 执行完记得关闭浏览器

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 12:57:50