Ubuntu ARM服务器上TypeScript调用Selenium+Chromium的配置问题
Ubuntu ARM服务器上TypeScript配置Selenium调用Chromium的正确方法
问题背景
在Ubuntu ARM服务器上已安装Selenium和Chromium,Python测试代码可正常运行,但TypeScript项目中使用forBrowser('chrome')无法调用Chromium,尝试设置Chrome二进制路径、ServiceBuilder创建会话等方式均无效。
错误尝试代码
尝试代码1
let driver: WebDriver|null = null let options = new chrome.Options() options.addArguments('--headless=new') options.addArguments('--no-sandbox') options.addArguments('--disable-dev-shm-usage') driver = new Builder() .forBrowser('chrome') .setChromeOptions(options) .build()
错误的路径设置
// 错误:将chromedriver路径赋值给浏览器二进制路径参数 options.setChromeBinaryPath('/usr/lib/chromium-browser/chromedriver')
尝试代码2
let driver: WebDriver|null = null const options = new chrome.Options() options.addArguments('--headless=new') options.addArguments('--no-sandbox') options.addArguments('--disable-dev-shm-usage') const service = new chrome.ServiceBuilder() .setPath('/usr/lib/chromium-browser/chromedriver') .build() driver = chrome.Driver.createSession(options, service)
可用的Python参考代码
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service as ChromiumService from webdriver_manager.chrome import ChromeDriverManager from webdriver_manager.core.os_manager import ChromeType service = ChromiumService(executable_path="/usr/lib/chromium-browser/chromedriver") options = Options() options.add_argument('--headless=new') options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') driver = webdriver.Chrome(service=service, options=options) driver.get("https://python.org") print(driver.title) driver.close()
正确配置方法
核心误区纠正
setChromeBinaryPath用于指定Chromium浏览器的可执行文件路径,而非chromedriver驱动程序的路径。Ubuntu ARM环境下,Chromium浏览器默认路径为/usr/bin/chromium-browser,chromedriver路径为/usr/lib/chromium-browser/chromedriver,二者不可混淆。
方案1:使用Builder初始化驱动
import { Builder } from 'selenium-webdriver'; import * as chrome from 'selenium-webdriver/chrome'; let driver: chrome.WebDriver | null = null; async function initDriver() { const options = new chrome.Options() .addArguments('--headless=new') .addArguments('--no-sandbox') .addArguments('--disable-dev-shm-usage') // 指定Chromium浏览器二进制文件路径 .setChromeBinaryPath('/usr/bin/chromium-browser'); // 指定chromedriver服务路径 const service = new chrome.ServiceBuilder('/usr/lib/chromium-browser/chromedriver').build(); driver = await new Builder() .forBrowser('chrome') .setChromeOptions(options) .setChromeService(service) .build(); // 测试访问 await driver.get('https://python.org'); console.log(await driver.getTitle()); await driver.quit(); } initDriver().catch(err => console.error(err));
方案2:直接创建会话
import * as chrome from 'selenium-webdriver/chrome'; let driver: chrome.WebDriver | null = null; async function initDriver() { const options = new chrome.Options() .addArguments('--headless=new') .addArguments('--no-sandbox') .addArguments('--disable-dev-shm-usage') .setChromeBinaryPath('/usr/bin/chromium-browser'); const service = new chrome.ServiceBuilder('/usr/lib/chromium-browser/chromedriver').build(); driver = await chrome.Driver.createSession(options, service); await driver.get('https://python.org'); console.log(await driver.getTitle()); await driver.quit(); } initDriver().catch(err => console.error(err));
额外检查项
- 版本匹配:确保chromedriver版本与Chromium完全一致(Selenium 4.x要求严格版本对应),可通过
chromium-browser --version和chromedriver --version查看。 - 权限设置:执行
chmod +x /usr/lib/chromium-browser/chromedriver确保驱动程序拥有可执行权限。 - ARM架构适配:确认安装的chromedriver为ARM64版本,避免安装x86版本导致无法运行。
内容的提问来源于stack exchange,提问作者Just Me
相关产品推荐
相关产品推荐

