解决Selenuim报错:unknown error: Chrome failed to start: exited abnormally.

Spring Wu 73 2022-06-11

起因

在本地使用Selenuim动态爬取页面时,没有任何问题,当部署到Ubuntu服务器上后报错
image

解决,根据在Stack Overflow搜索到的答案,debug出来能在Ubuntu上运行的配置如下:

options.setHeadless(true);
// 这一行是解决问题的关键
options.setExperimentalOption("useAutomationExtension", false);
options.addArguments("start-maximized"); // open Browser in maximized mode
options.addArguments("disable-infobars"); // disabling infobars
options.addArguments("--disable-extensions"); // disabling extensions
options.addArguments("--disable-gpu"); // applicable to windows os only
options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
options.addArguments("--no-sandbox");
options.addArguments("--headless");

webdriver连接池做了如下调整

ChromeOptions options = new ChromeOptions();
if (StrUtil.containsIgnoreCase(activeProfile, "dev")) {
   options.addArguments("--start-maximized");
   options.addArguments("--disable-gpu");
   options.addArguments("--allow-running-insecure-content");
   options.addArguments("lang=en_US");
} else {
   options.setHeadless(true);
   options.setExperimentalOption("useAutomationExtension", false);
   options.addArguments("start-maximized"); // open Browser in maximized mode
   options.addArguments("disable-infobars"); // disabling infobars
   options.addArguments("--disable-extensions"); // disabling extensions
   options.addArguments("--disable-gpu"); // applicable to windows os only
   options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
   options.addArguments("--no-sandbox");
   options.addArguments("--headless");
}

# Selenuim # 爬虫