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

使用Python Selenium无法访问Chrome Dino恐龙游戏的问题求助

问题原因

你在初始化WebDriver的时候就提前将网络设置为离线状态,Selenium的driver.get()方法默认会等待页面加载的网络响应返回成功状态才会结束执行,即便chrome://dino是Chrome的本地内置页面,离线状态下的访问也会被Selenium判定为网络异常,进而抛出对应的报错。

解决方案

方案1:调整执行顺序(推荐)

先加载chrome://dino页面,再设置离线状态触发游戏,修改后的代码参考如下:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import WebDriverException


class GameControl():
    def __init__(self):
        options = webdriver.ChromeOptions()
        options.add_experimental_option('excludeSwitches', ['enable-logging'])
        options.add_experimental_option("detach", True)
        self.DINO_URL = "chrome://dino"
        self.driver = webdriver.Chrome(options=options)
        self.actionChains = ActionChains(self.driver)

    def load_game(self):
        # 先在线状态加载内置游戏页面
        self.driver.get(self.DINO_URL)
        # 加载完成后再设置离线,触发游戏可运行状态
        self.driver.set_network_conditions(offline=True, latency=5, throughput=500 * 1024)

    def close_game(self):
        self.driver.close()

    def start_game(self):
        print("Starting game...")
        self.actionChains.send_keys(Keys.SPACE).perform()

方案2:直接捕获网络异常

如果你需要提前设置离线状态,也可以直接忽略该异常——抛出报错时chrome://dino实际上已经完成渲染,不影响后续操作,修改load_game方法即可:

def load_game(self):
    try:
        self.driver.get(self.DINO_URL)
    except WebDriverException:
        # 离线状态下的报错可直接忽略,页面已正常显示恐龙游戏
        pass

两种方案执行完成后,调用start_game()都可以正常触发恐龙跳跃,启动游戏。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 04:27:01