Ubuntu运行Node.js时遇Server terminated early with status 127错误求助
解决Selenium executeScript报错:Server terminated early with status 127
问题描述
运行TypeScript编写的Selenium代码时,调用executeScript出现Server terminated early with status 127错误。已安装Java 20,且chromedriver与Chrome版本匹配(日志显示均为116.0.5845.96)。
代码示例
import * as chrome from 'selenium-webdriver/chrome'; import { Builder } from 'selenium-webdriver'; async function execute(url: string): Promise<string> { const options = new chrome.Options().headless(); options.addArguments("--no-sandbox"); const driver = new Builder() .forBrowser('chrome') .setChromeOptions(options) .build(); driver.get(url); console.log(`Finish Initializing options and driver ... `) const response: string = await driver.executeScript( 'const msg = "Hallo, world!"; console.log(`${msg}`); return `${msg}`' ); console.log(`Execute script successfully ... `) await driver.quit(); return response; } async function call() { const response = await execute("https://google.com"); console.info(`Response: ${response}`) } call()
错误日志
Selenium Manager binary found at /home/work/node_modules/selenium-webdriver/bin/linux/selenium-manager Driver path: /home/.cache/selenium/chromedriver/linux64/116.0.5845.96/chromedriver Browser path: /home/.cache/selenium/chrome/linux64/116.0.5845.96/chrome Finish Initializing options and driver ... Error: Error: Server terminated early with status 127
解决方案
- 检查Chrome依赖库完整性:状态码127通常意味着Chrome缺少运行依赖,在Linux环境下尤为常见。执行以下命令查找缺失的库:
根据输出安装对应依赖(例如ldd /home/.cache/selenium/chrome/linux64/116.0.5845.96/chrome | grep not foundlibX11、libXext、libXrender、libnss3等)。 - 更新Chrome启动参数:替换旧版无头模式,添加更多兼容性参数,修改后的options代码:
const options = new chrome.Options().addArguments( "--headless=new", // 使用新版无头模式,兼容性更好 "--no-sandbox", "--disable-dev-shm-usage", // 避免共享内存空间不足问题 "--remote-debugging-port=9222" // 强制启用调试端口,避免启动失败 ); - 确保chromedriver有执行权限:执行命令赋予可执行权限:
chmod +x /home/.cache/selenium/chromedriver/linux64/116.0.5845.96/chromedriver - 显式指定Chrome二进制路径:如果Selenium Manager自动下载的Chrome版本有问题,手动指定系统中已安装的Chrome路径:
options.setChromeBinaryPath('/usr/bin/google-chrome'); // 替换为实际路径 - 开启详细日志排查:添加日志配置,捕获更多错误细节:
import { logging } from 'selenium-webdriver'; const driver = new Builder() .forBrowser('chrome') .setChromeOptions(options) .setLoggingPrefs({ browser: logging.Level.ALL, driver: logging.Level.ALL }) .build();
内容的提问来源于stack exchange,提问作者Dang Huy Nguyen
相关产品推荐
相关产品推荐

