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

Linux下ChromeDriver控制Chrome时Web Speech API语音合成失效求助

我之前刚好碰到过一模一样的问题!当时折腾了好一阵才搞定,给你几个可行的方向试试:

可能的解决方案

1. 确保ChromeDriver与Chrome版本完全匹配

这是最容易忽略但又关键的点——版本不兼容经常导致各类Web API异常。你可以通过以下命令检查版本:

  • 查看ChromeDriver版本:chromedriver --version
  • 查看Chrome浏览器版本:在地址栏输入chrome://version/
    两者的主版本号(比如118.x.y.z)必须完全一致,不一致的话去下载对应版本的ChromeDriver替换即可。

2. 添加Chrome启动参数

Linux下用Selenium启动Chrome时,默认环境可能缺少语音服务相关的配置,试试添加这些启动参数:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
# 强制启用语音调度器
options.add_argument("--enable-speech-dispatcher")
# 跳过媒体权限弹窗(权限限制可能阻碍语音服务加载)
options.add_argument("--use-fake-ui-for-media-stream")
# 部分Linux环境下GPU加速会干扰语音服务
options.add_argument("--disable-gpu")

driver = webdriver.Chrome(options=options)

3. 等待语音服务异步加载完成

window.speechSynthesis.getVoices()是异步加载的,手动启动Chrome时浏览器已经完成加载,但Selenium启动后可能还没就绪。你可以通过监听事件等待语音列表加载:

// 可通过Selenium注入这段脚本执行
function waitForVoicesReady() {
  return new Promise((resolve) => {
    const voices = window.speechSynthesis.getVoices();
    if (voices.length > 0) {
      resolve(voices);
      return;
    }
    window.speechSynthesis.onvoiceschanged = () => {
      resolve(window.speechSynthesis.getVoices());
    };
  });
}

// 等语音加载完成后再执行播放逻辑
waitForVoicesReady().then((voices) => {
  console.log("可用语音列表:", voices);
  const utterance = new SpeechSynthesisUtterance("测试文本");
  utterance.voice = voices[0]; // 指定第一个可用语音
  window.speechSynthesis.speak(utterance);
});

在Selenium中可以用driver.execute_script(上述代码)来执行这段逻辑。

4. 检查Linux系统的语音依赖

Chrome的语音合成依赖系统的speech-dispatcher服务,你可以先确认系统是否安装了必要组件:

  • Ubuntu/Debian系:执行sudo apt install speech-dispatcher espeak
  • 安装完成后可以在终端测试spd-say "测试语音",如果能正常播放说明系统层面的语音服务没问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:53:11