![](/img/trans.png)
[英]Selenium Grid: DevToolsActivePort file doesn't exist (unknown error: Chrome failed to start: exited abnormally)
[英]unknown error: DevToolsActivePort file doesn't exist error while executing Selenium UI test cases on ubuntu
我有一個 ubuntu 服務器也有 UI。 你可以通過觸發 mvn test 命令來執行測試用例。 但問題是當我通過另一台機器的終端執行機器的 ssh 時,我收到以下錯誤 -
unknown error: DevToolsActivePort file doesn't exist
(Driver info: chromedriver=2.40.565383 (76257d1ab79276b2d53ee976b2c3e3b9f335cde7),platform=Linux 4.4.0-121-generic x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 60.04 seconds
Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T18:33:54.468Z'
System info: host: 'ubuntu-test', ip: 'X.X.X.X', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-121-generic', java.version: '1.8.0_171'
Driver info: driver.version: ChromeDriver
但是如果我通過 remmina 遠程控制機器然后執行這台機器終端的相同命令,則相同的命令會成功啟動 chrome。
這個錯誤信息...
unknown error: DevToolsActivePort file doesn't exist
...暗示ChromeDriver無法啟動/生成新的WebBrowser,即Chrome 瀏覽器會話。
你的代碼試驗和所有二進制文件的版本信息會給我們一些關於出了什么問題的提示。
但是,根據將 --disable-dev-shm-usage添加到默認啟動標志,似乎添加參數--disable-dev-shm-usage
將暫時解決該問題。
如果您希望啟動/跨越新的Chrome 瀏覽器會話,您可以使用以下Java解決方案:
System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
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("--no-sandbox"); // Bypass OS security model
WebDriver driver = new ChromeDriver(options);
driver.get("https://google.com");
根據base_switches.cc disable-dev-shm-usage
似乎僅在Linix OS上有效:
#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
// The /dev/shm partition is too small in certain VM environments, causing
// Chrome to fail or crash (see http://crbug.com/715363). Use this flag to
// work-around this issue (a temporary directory will always be used to create
// anonymous shared memory files).
const char kDisableDevShmUsage[] = "disable-dev-shm-usage";
#endif
在討論中添加一個選項以使用 /tmp 而不是 /dev/shm David 提到:
我認為這取決於 /dev/shm 和 /tmp 的安裝方式。 如果它們都安裝為 tmpfs,我假設不會有任何區別。 如果由於某種原因 /tmp 沒有映射為 tmpfs(我認為 systemd 默認映射為 tmpfs),chrome 共享內存管理在創建匿名共享文件時總是將文件映射到內存中,所以即使在這種情況下也不應該差別很大。 我想你可以在啟用標志的情況下強制進行遙測測試,看看它是如何進行的。
至於為什么不默認使用,這是共享內存團隊的推論,我想默認情況下應該使用 /dev/shm 作為共享內存是有道理的。
最終所有這些都應該轉向使用 memfd_create,但我認為這不會很快發生,因為它需要顯着重構 Chrome 內存管理。
您可以在以下位置找到一些詳細的討論:
這是沙盒故事的鏈接。
當我嘗試在無頭模式下硒 UI 測試用例時,我遇到了類似的問題。 這是因為我沒有顯示服務器。 啟動 Xvfb 對我有用。
sudo yum -y install Xvfb libXfont Xorg
sudo yum -y groupinstall "X Window System" "Desktop" "Fonts" "General Purpose Desktop"
Xvfb :99 -screen 0 1024x768x24 &
export DISPLAY=:1
嘗試在沒有sudo
-privileges 的情況下運行 selenium-server:
java -jar path/to/selenium-server-standalone.jar
如果您在沒有 X-forward 的情況下從 ssh 運行,您的 chrome 瀏覽器將崩潰。 為了防止這種情況,您可以使用 DebanjanB 發布的選項,最重要的是 --headless。 如果以 root 身份運行(不推薦),您還需要 --no-sandbox。
當我將舊版本的 selenium-java (3.5.3) 與較新的 chromedriver (75.x) 一起使用時,我也遇到了這個錯誤。 將 chromedriver 的 2.46 版本與 3.5.3 或 75.x 與 3.141.59 的 selenium java 一起使用對我有用。
運行本地 Xvfb 也應該可以,但我建議使用 headless,它可以更快。
檢查建議的重復帖子,請更新並標記為已解決任何對您有幫助的內容。
當我使用 cron 作業運行 selenium 時,我遇到了同樣的問題。 經過長時間的痛苦,我找到了解決它的方法。 只需將此行添加到 shell 腳本的開頭:
export DISPLAY=:1
以上都沒有奏效。 唯一的解決方案是從以下位置使用驅動程序:
'/snap/bin/chromium.chromedriver'
這個問題解釋了它: https : //stackoverflow.com/a/61980562/1568464
我使用 python 使用此配置
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("no-sandbox")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--headless")
driver = os.path.join("path/of/driver","chromedriver")
browser = webdriver.Chrome(executable_path=driver,chrome_options=chrome_options)
browser.get("https://www.example.com")
print(browser.title)
嘗試使用此方法來實例化 chrome web 驅動程序可以幫助您在 ubuntu 中解決此問題:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opt = Options()
opt.add_argument("--no-sandbox")
opt.add_argument("--disable-dev-shm-usage")
driver = webdriver.Chrome(chrome_options=opt,
executable_path='<your-chromedriver-path>')
driver.get('https://www.google.com/')
感謝 @ George Pantazes對答案的評論
並確保 env 變量DISPLAY
已設置為您將啟動 chrome 瀏覽器的終端中的現有會話。
我一直在使用 Selenium 的 chrome 嵌入式應用程序(例如 CEF 或電子應用程序)中專門收到此錯誤。
使用參數--headless和--no-sandbox和--disable-gpu不是解決方案。
我的問題的原因是電子和 CEF 應用程序。 他們沒有將所有 chrome 命令行開關轉發到其中正在運行的 Chrome 實例上,因此沒有創建 DevToolActivePort 文件。
我在這里發布了一個手動流程,供我回答另一個類似問題 -> https://stackoverflow.com/a/62545820/8708890 。 您可以按照此手動流程進行操作,看看它是否能解決您的問題。
它發生在我使用無頭 chrome 驅動程序並嘗試將窗口大小設置為 1366x768 或 1600x900 時。 我只能通過返回 1920x1080 來修復它。
希望它對某人有幫助
我將它作為 GitHub Action 運行,但沒有添加options.addArguments("--headless")
。 一旦我把它放進去,故障就消失了。 該線程中的各種其他建議均無濟於事。
我知道這是我的愚蠢,但我認為澄清它可能會有所幫助,因為在這種情況下因果之間的聯系並不清楚。
因為@DebanjanB 答案是正確的,但不清楚。
在修復問題和總結解決方案后:
代碼:
from selenium import webdriver
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument('--headless’)
driver = webdriver.Chrome(options=chromeOptions)
運行但錯誤:
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
(unknown error: 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.)
直接原因
unknown error: DevToolsActivePort file doesn't exist
意味着 ChromeDriver 生成 WebBrowser(Chrome 瀏覽器會話)失敗根本原因
使用普通用戶運行 Chrome
我的情況:在 CentOS 中
(1) 將 chrome 二進制文件更改為普通用戶擁有
sudo chown limao:limao /opt/google/chrome/google-chrome
注意:這里的chrome二進制文件是/usr/bin/google-chrome
經過幾個軟鏈接,真正的一個是/opt/google/chrome/google-chrome
更改所有者后:
limao@localhost:~/dev/ShortLinkParseServer/logs $ ll /usr/bin/google-chrome
lrwxrwxrwx 1 root root 31 Aug 4 16:30 /usr/bin/google-chrome -> /etc/alternatives/google-chrome
limao@localhost:~/dev/ShortLinkParseServer/logs $ ll /etc/alternatives/google-chrome
lrwxrwxrwx 1 root root 29 Aug 4 16:30 /etc/alternatives/google-chrome -> /usr/bin/google-chrome-stable
limao@localhost:~/dev/ShortLinkParseServer/logs $ ll /opt/google/chrome/google-chrome
-rwxr-xr-x 1 limao limao 1.9K Jul 31 04:46 /opt/google/chrome/google-chrome
(2)為supervisord spawn程序設置普通用戶
將user=xxx
添加到您的supervisord config file
,如下所示:
$ cat /etc/supervisord.d/supervisord_ShortLinkParseServer.conf
[program:ShortLinkParseServer]
command=/xxx/gunicorn/gunicorn_config.py app:app
...
; use normal user instead default root use, to avoid later chrome exception: unknown error: DevToolsActivePort file doesn't exist
user=limao
...
添加標志--disable-dev-shm-usage
chromeOptions.add_argument('--disable-dev-shm-usage')
太多人使用這個:添加標志--no-sandbox
但--no-sandbox
只是意味着不使用沙箱
-> 真正的效果只是繞過操作系統安全模型
-> 強烈不推薦使用--no-sandbox
我在運行 gitlab-runner 時遇到了這個問題。 在主顯示控制台,運行 xhost + 以允許遠程顯示訪問:
xhost +
在 .gitlab-ci.yml 中,我設置了 DISPLAY(假設它是:10.0):
export DISPLAY=:10.0
對我來說,問題是 /tmp/Crashpad 文件夾歸 root 所有,我的用戶 (centos) 無法寫入。 我做了以下,一切正常:
chmod 664 -R /tmp/Crashpad
我設法通過嘗試直接從終端運行google-chrome
二進制文件來調試它,因為我試圖用(centos)運行 selenium 的用戶。
這樣做,我得到了以下幫助我調試的錯誤:
[19:11][centos@my.computer.com ~]$ /usr/bin/google-chrome --headless
[0110/191116.894836:ERROR:filesystem_posix.cc(63)] mkdir /tmp/Crashpad/new: Permission denied (13)
只需使用無頭模式
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
driver = new ChromeDriver(options);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.