一、安装Google Chrome
如果服务器可以翻墙的话,可以直接使用官方文档(https://www.ubuntuupdates.org/ppa/google_chrome)中的方法,增加Chrome的源,然后直接使用apt-get自动安装依赖即可。
如果服务器无法翻墙,则必须自行下载deb安装包:https://www.ubuntuupdates.org/package/google_chrome/stable/main/base/google-chrome-stable
注意,这里需要根据chromedriver的版本选择合适的Chrome版本,我们使用的是:google-chrome-stable_80.0.3987.106-1_amd64.deb。
将安装包上传到服务器,然后运行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
root@5mfg7nd7zkZ:~# sudo dpkg -i ./google-chrome*.deb Selecting previously unselected package google-chrome-stable. (Reading database ... 128555 files and directories currently installed.) Preparing to unpack .../google-chrome-stable_80.0.3987.106-1_amd64.deb ... Unpacking google-chrome-stable (80.0.3987.106-1) ... dpkg: dependency problems prevent configuration of google-chrome-stable: google-chrome-stable depends on fonts-liberation; however: Package fonts-liberation is not installed. google-chrome-stable depends on libappindicator3-1; however: Package libappindicator3-1 is not installed. google-chrome-stable depends on libasound2 (>= 1.0.16); however: Package libasound2 is not installed. google-chrome-stable depends on libatk-bridge2.0-0 (>= 2.5.3); however: Package libatk-bridge2.0-0 is not installed. google-chrome-stable depends on libatspi2.0-0 (>= 2.9.90); however: Package libatspi2.0-0 is not installed. google-chrome-stable depends on libgtk-3-0 (>= 3.9.10); however: Package libgtk-3-0 is not installed. google-chrome-stable depends on libx11-xcb1; however: Package libx11-xcb1 is not installed. google-chrome-stable depends on xdg-utils (>= 1.0.2); however: Package xdg-utils is not installed. dpkg: error processing package google-chrome-stable (--install): dependency problems - leaving unconfigured Processing triggers for mime-support (3.60ubuntu1) ... Processing triggers for man-db (2.8.3-2ubuntu0.1) ... Errors were encountered while processing: google-chrome-stable |
对于这些依赖错误其实没必要手工安装:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
root@iZm5efmfg71vv8eg5nd7zkZ:~# sudo apt-get install xdg-utils libx11-xcb1 fonts-liberation libasound2 Reading package lists... Done Building dependency tree Reading state information... Done You might want to run 'apt --fix-broken install' to correct these. The following packages have unmet dependencies: google-chrome-stable : Depends: libappindicator3-1 but it is not going to be installed Depends: libatk-bridge2.0-0 (>= 2.5.3) but it is not going to be installed Depends: libatspi2.0-0 (>= 2.9.90) but it is not going to be installed Depends: libgtk-3-0 (>= 3.9.10) but it is not going to be installed Recommends: libu2f-udev but it is not going to be installed libasound2 : Depends: libasound2-data (>= 1.1.3-5ubuntu0.2) but it is not going to be installed E: Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution). |
可见直接根据提示使用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
root@iZm5efmfg71vv8eg5nd7zkZ:~# apt --fix-broken install Reading package lists... Done Building dependency tree Reading state information... Done Correcting dependencies... Done The following packages were automatically installed and are no longer required: java-common libjemalloc1 libopts25 libpcsclite1 redis-server redis-tools sntp Use 'apt autoremove' to remove them. The following additional packages will be installed: at-spi2-core dconf-gsettings-backend dconf-service fonts-liberation glib-networking glib-networking-common glib-networking-services gsettings-desktop-schemas libappindicator3-1 libasound2 libasound2-data libatk-bridge2.0-0 libatspi2.0-0 libauthen-sasl-perl libcairo-gobject2 libcolord2 libdata-dump-perl libdbusmenu-gtk3-4 libdconf1 libdrm-amdgpu1 libdrm-common libdrm-intel1 libdrm-nouveau2 libdrm-radeon1 libdrm2 libencode-locale-perl libepoxy0 libfile-basedir-perl libfile-desktopentry-perl libfile-listing-perl libfile-mimeinfo-perl libfont-afm-perl libfontenc1 libgl1 libgl1-mesa-dri libgl1-mesa-glx libglapi-mesa libglvnd0 libglx-mesa0 libglx0 libgtk-3-0 libgtk-3-bin libgtk-3-common libhtml-form-perl libhtml-format-perl libhtml-parser-perl libhtml-tagset-perl libhtml-tree-perl libhttp-cookies-perl libhttp-daemon-perl libhttp-date-perl libhttp-message-perl libhttp-negotiate-perl libice6 libindicator3-7 libio-html-perl libio-socket-ssl-perl libipc-system-simple-perl libjson-glib-1.0-0 libjson-glib-1.0-common libllvm9 liblwp-mediatypes-perl liblwp-protocol-https-perl libmailtools-perl libnet-dbus-perl libnet-http-perl libnet-smtp-ssl-perl libnet-ssleay-perl libpciaccess0 libproxy1v5 librest-0.7-0 libsm6 libsoup-gnome2.4-1 libsoup2.4-1 libtie-ixhash-perl libtry-tiny-perl liburi-perl libwayland-client0 libwayland-cursor0 libwayland-egl1 libwww-perl libwww-robotrules-perl libx11-protocol-perl libx11-xcb1 libxaw7 libxcb-dri2-0 libxcb-dri3-0 libxcb-glx0 libxcb-present0 libxcb-shape0 libxcb-sync1 libxft2 libxkbcommon0 libxml-parser-perl libxml-twig-perl libxml-xpathengine-perl libxmu6 libxshmfence1 libxt6 libxv1 libxxf86dga1 libxxf86vm1 perl-openssl-defaults x11-utils x11-xserver-utils xdg-utils Suggested packages: indicator-application libasound2-plugins alsa-utils libdigest-hmac-perl libgssapi-perl colord gvfs libcrypt-ssleay-perl libauthen-ntlm-perl libunicode-map8-perl libunicode-string-perl xml-twig-tools mesa-utils nickle cairo-5c xorg-docs-core The following NEW packages will be installed: at-spi2-core dconf-gsettings-backend dconf-service fonts-liberation glib-networking glib-networking-common glib-networking-services gsettings-desktop-schemas libappindicator3-1 libasound2 libasound2-data libatk-bridge2.0-0 libatspi2.0-0 libauthen-sasl-perl libcairo-gobject2 libcolord2 libdata-dump-perl libdbusmenu-gtk3-4 libdconf1 libdrm-amdgpu1 libdrm-intel1 libdrm-nouveau2 libdrm-radeon1 libencode-locale-perl libepoxy0 libfile-basedir-perl libfile-desktopentry-perl libfile-listing-perl libfile-mimeinfo-perl libfont-afm-perl libfontenc1 libgl1 libgl1-mesa-dri libgl1-mesa-glx libglapi-mesa libglvnd0 libglx-mesa0 libglx0 libgtk-3-0 libgtk-3-bin libgtk-3-common libhtml-form-perl libhtml-format-perl libhtml-parser-perl libhtml-tagset-perl libhtml-tree-perl libhttp-cookies-perl libhttp-daemon-perl libhttp-date-perl libhttp-message-perl libhttp-negotiate-perl libice6 libindicator3-7 libio-html-perl libio-socket-ssl-perl libipc-system-simple-perl libjson-glib-1.0-0 libjson-glib-1.0-common libllvm9 liblwp-mediatypes-perl liblwp-protocol-https-perl libmailtools-perl libnet-dbus-perl libnet-http-perl libnet-smtp-ssl-perl libnet-ssleay-perl libpciaccess0 libproxy1v5 librest-0.7-0 libsm6 libsoup-gnome2.4-1 libsoup2.4-1 libtie-ixhash-perl libtry-tiny-perl liburi-perl libwayland-client0 libwayland-cursor0 libwayland-egl1 libwww-perl libwww-robotrules-perl libx11-protocol-perl libx11-xcb1 libxaw7 libxcb-dri2-0 libxcb-dri3-0 libxcb-glx0 libxcb-present0 libxcb-shape0 libxcb-sync1 libxft2 libxkbcommon0 libxml-parser-perl libxml-twig-perl libxml-xpathengine-perl libxmu6 libxshmfence1 libxt6 libxv1 libxxf86dga1 libxxf86vm1 perl-openssl-defaults x11-utils x11-xserver-utils xdg-utils The following packages will be upgraded: libdrm-common libdrm2 2 upgraded, 104 newly installed, 0 to remove and 264 not upgraded. 2 not fully installed or removed. Need to get 24.0 MB/32.7 MB of archives. After this operation, 340 MB of additional disk space will be used. Do you want to continue? [Y/n] y |
将依赖问题修复,然后再次安装deb即可:
1 2 3 4 5 6 7 8 |
root@iZm5efmfg71vv8eg5nd7zkZ:~# sudo dpkg -i ./google-chrome*.deb (Reading database ... 130333 files and directories currently installed.) Preparing to unpack .../google-chrome-stable_80.0.3987.106-1_amd64.deb ... Unpacking google-chrome-stable (80.0.3987.106-1) over (80.0.3987.106-1) ... Setting up google-chrome-stable (80.0.3987.106-1) ... Processing triggers for mime-support (3.60ubuntu1) ... Processing triggers for man-db (2.8.3-2ubuntu0.1) ... root@iZm5efmfg71vv8eg5nd7zkZ:~# |
然后我们使用如下命令进行测试:
1 2 3 4 5 |
root@iZm5efmfg71vv8eg5nd7zkZ:~# google-chrome --headless --remote-debugging-port=17654 https://www.baidu.com --disable-gpu [0216/191118.579248:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180. root@iZm5efmfg71vv8eg5nd7zkZ:~# google-chrome --headless --remote-debugging-port=17654 https://www.baidu.com --disable-gpu --no-sandbox DevTools listening on ws://127.0.0.1:17654/devtools/browser/cb0a5695-d8c9-4388-ae32-07521ddf33d8 |
由于服务器环境下可能没有GPU,所以使用–disable-gpu以免报错。然后访问Chrome打开的本地的17654端口:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
root@iZm5efmfg71vv8eg5nd7zkZ:~# curl 127.0.0.1:17654 <html> <head> <title>Headless remote debugging</title> <style> </style> <script> const fetchjson = (url) => fetch(url).then(r => r.json()); function loadData() { const getList = fetchjson("/json/list"); const getVersion = fetchjson('/json/version'); Promise.all([getList, getVersion]).then(parseResults); } function parseResults([listData, versionData]){ const version = versionData['WebKit-Version']; const hash = version.match(/\s\(@(\b[0-9a-f]{5,40}\b)/)[1]; listData.forEach(item => appendItem(item, hash)); } function appendItem(item, hash) { let link; if (item.devtoolsFrontendUrl) { link = document.createElement("a"); var devtoolsFrontendUrl = item.devtoolsFrontendUrl.replace(/^\/devtools\//,''); link.href = `https://chrome-devtools-frontend.appspot.com/serve_file/@${hash}/${devtoolsFrontendUrl}&remoteFrontend=true`; link.title = item.title; } else { link = document.createElement("div"); link.title = "The tab already has active debugging session"; } var text = document.createElement("div"); if (item.title) text.textContent = item.title; else text.textContent = "(untitled tab)"; if (item.faviconUrl) text.style.cssText = "background-image:url(" + item.faviconUrl + ")"; link.appendChild(text); var p = document.createElement("p"); p.appendChild(link); document.getElementById("items").appendChild(p); } </script> </head> <body onload='loadData()'> <div id='caption'>Inspectable WebContents</div> <div id='items'></div> </body> </html> root@iZm5efmfg71vv8eg5nd7zkZ:~# |
能够看到调试信息则说明Chrome安装好了,此即为Chrome的远程调试模式(下图为Ubunut Desktop 18.04 LTS):
二、安装chromedriver
chromedriver提供了操作Chrome的API,是Selenium控制Chrome的桥梁。从官网https://chromedriver.chromium.org/downloads下载与Chrome浏览器匹配的版本并解压即可,解压后得到的是一个名为chromedriver的可执行文件:
1 2 3 4 5 6 7 8 9 |
root@iZm5efmfg71vv8eg5nd7zkZ:~# unzip chromedriver_linux64.zip Archive: chromedriver_linux64.zip inflating: chromedriver root@iZm5efmfg71vv8eg5nd7zkZ:~# ll total 77296 drwx------ 41 root root 4096 Feb 16 19:19 ./ drwxr-xr-x 22 root root 4096 Dec 30 11:27 ../ -rwxrwxr-x 1 root root 10251496 Feb 14 03:21 chromedriver* -rw-r--r-- 1 root root 4943146 Feb 16 19:18 chromedriver_linux64.zip |
三、Java中的使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import org.junit.Test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; public class ChromeHeadlessTest { @Test public void test() { try { System.setProperty("webdriver.chrome.driver", "E:\\chromedriver_win32\\chromedriver.exe"); ChromeOptions chromeOptions = new ChromeOptions(); chromeOptions.addArguments("--no-sandbox"); chromeOptions.addArguments("--disable-infobars"); chromeOptions.addArguments("--disable-dev-shm-usage"); chromeOptions.addArguments("--disable-browser-side-navigation"); chromeOptions.addArguments("--disable-gpu"); chromeOptions.addArguments("--headless"); WebDriver webDriver = new ChromeDriver(chromeOptions); webDriver.get("http://www.baidu.com"); webDriver.close(); } catch (Exception e) { } finally { } } } |
参考资料:
1、https://askubuntu.com/questions/79280/how-to-install-chrome-browser-properly-via-command-line
转载时请保留出处,违法转载追究到底:进城务工人员小梅 » Ubunut18.04 LTS服务器版上Chrome Headless的安装及使用(Java)