banner
libxcnya.so

libxcnya.so

Nothing...
telegram
twitter
github
email

在 Linux 環境下部署 Node.JS + Selenium 專案踩坑

前言#

由於博主的 Windows 伺服器快過期了,所以之前跑在 Windows 機子上的東西得考慮往 Linux 上遷移
然後試圖跑小學二年級寫的 Shit Code 發現跑不起來
然後就有了今天的這篇水文

安裝#

安裝 Google Chrome

wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
apt install ./google-chrome-stable_current_amd64.deb -y
rm google-chrome-stable_current_amd64.deb

(有一說一這地址在國內沒牆)

輸入 google-chrome --version 驗證是否安裝成功

1

如果正常輸出版本就代表安裝成功了

在 NPM 中,執行 npm install selenium-webdriver 理論上就能配好在 node 專案中使用的 chromedriver
就拿上個月寫的答辯 Hostloc 刷分的小玩意舉個例子吧
當時哥們所有的東西都裝好後一跑就報錯

2

root@NekoServer:~# node main.js

=.=

An error occurred: session not created: Chrome failed to start: exited normally.
  (session not created: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
/root/node_modules/selenium-webdriver/lib/error.js:524
    let err = new ctor(data.message)
              ^

SessionNotCreatedError: session not created: Chrome failed to start: exited normally.
  (session not created: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
    at Object.throwDecodedError (/root/node_modules/selenium-webdriver/lib/error.js:524:15)
    at parseHttpResponse (/root/node_modules/selenium-webdriver/lib/http.js:601:13)
    at Executor.execute (/root/node_modules/selenium-webdriver/lib/http.js:529:28)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  remoteStacktrace: '#0 0x5611b9842f83 <unknown>\n' +
    '#1 0x5611b94fbcf7 <unknown>\n' +
    '#2 0x5611b953360e <unknown>\n' +
    '#3 0x5611b953026e <unknown>\n' +
    '#4 0x5611b958080c <unknown>\n' +
    '#5 0x5611b9574e53 <unknown>\n' +
    '#6 0x5611b953cdd4 <unknown>\n' +
    '#7 0x5611b953e1de <unknown>\n' +
    '#8 0x5611b9807531 <unknown>\n' +
    '#9 0x5611b980b455 <unknown>\n' +
    '#10 0x5611b97f3f55 <unknown>\n' +
    '#11 0x5611b980c0ef <unknown>\n' +
    '#12 0x5611b97d799f <unknown>\n' +
    '#13 0x5611b9830008 <unknown>\n' +
    '#14 0x5611b98301d7 <unknown>\n' +
    '#15 0x5611b9842124 <unknown>\n' +
    '#16 0x7f9436572609 start_thread\n'
}

Node.js v18.17.1
root@NekoServer:~#

一時以為是 chromedriver 的問題,然後試圖手動安裝 chromedriver

wget https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/120.0.6099.109/linux64/chromedriver-linux64.zip
unzip chromedriver-linux64.zip
mv chromedriver-linux64/chromedriver /usr/bin/
rm -rf chromedriver-linux64
chromedriver --version

(最新版 ChromeDriver 可以在 這裡 找到)

然後又跑了一遍,發現還是這問題
然後我手賤執行了一下 google-chrome 哦我明白了

3

原來這玩意正常情況下不能跑在 root 使用者底下,非要跑得加個參數 --no-sandbox

4

找到不知道幾年級寫的專案裡面把 Options CV 過來,然後加上一行 --no-sandbox,再跑一次

5

然後就能跑了
┐(´-`)┌

此文由 Mix Space 同步更新至 xLog
原始連結為 https://blog.nekorua.com/posts/technology/103.html


載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。