1、Linux機器安裝google-chrome-stablephp
一、設置google-chrome軟件源html
sudo vim /etc/yum.repos.d/google-chrome.repolinux
[google-chrome] name=google-chrome baseurl=http://dl.google.com/linux/chrome/rpm/stable/x86_64 enabled=1 gpgcheck=1 gpgkey=https://dl.google.com/linux/linux_signing_key.pub
二、安裝依賴[過程當中如提示有其餘依賴缺失,可至https://rpmfind.net/linux/rpm2html/search.php 查詢對應名字rpm包 執行sudo yum install -y 安裝便可]web
sudo yum install https://mirrors.dotsrc.org/fedora-epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm -y sudo yum install Xvfb -y sudo yum install xorg-x11-fonts* -y sudo yum install https://rpmfind.net/linux/fedora/linux/development/rawhide/Everything/x86_64/os/Packages/l/liberation-fonts-common-2.00.5-6.fc32.noarch.rpm -y sudo yum install https://rpmfind.net/linux/fedora/linux/development/rawhide/Everything/x86_64/os/Packages/l/liberation-mono-fonts-2.00.5-6.fc32.noarch.rpm -y sudo yum install https://rpmfind.net/linux/fedora/linux/development/rawhide/Everything/x86_64/os/Packages/l/liberation-sans-fonts-2.00.5-6.fc32.noarch.rpm -y sudo yum install https://rpmfind.net/linux/fedora/linux/development/rawhide/Everything/x86_64/os/Packages/l/liberation-serif-fonts-2.00.5-6.fc32.noarch.rpm -y sudo yum install -y https://rpmfind.net/linux/fedora-secondary/development/rawhide/Everything/ppc64le/os/Packages/l/liberation-fonts-2.00.5-6.fc32.noarch.rpm -y
三、安裝google-chrome-stablechrome
sudo yum install google-chrome-stable -y
2、準備客戶端【webdriver隨客戶端發放到服務器】vim
public static WebDriver driver;
/** * 初始化Driver * @throws Exception */ public void initDriver() throws Exception { String path; File file; String osName = System.getProperty("os.name").toLowerCase();
// webdriver 版本作持久化,driver初始化時載入內存,動態調用配置 if (getClass().getResource("/") == null) { String chromePath = "drivers/".concat(osName.startsWith("windows") ? ContextUtils.getContextStr(CHROME_DRIVER_WINDOWS) : osName.startsWith("mac") ? ContextUtils.getContextStr(CHROME_DRIVER_MAC) : ContextUtils.getContextStr(CHROME_DRIVER_LINUX)); path = String.format("%s%starget%s%s", System.getProperty("base.dir"), File.separator, File.separator, chromePath); file = new File(path); } else { String chromePath = "drivers/".concat(osName.startsWith("windows") ? ContextUtils.getContextStr(CHROME_DRIVER_WINDOWS) : osName.startsWith("mac") ? ContextUtils.getContextStr(CHROME_DRIVER_MAC) : ContextUtils.getContextStr(CHROME_DRIVER_LINUX)); path = String.format("%s%s%s", getClass().getResource("/").getPath(), File.separator, chromePath); file = new File(path); if (file.exists() == false) { FileUtils.copyInputStreamToFile(new ClassPathResource(chromePath).getInputStream(), new File(path)); } } if (!osName.startsWith("windows")) { try { Runtime.getRuntime().exec(String.format("chmod 777 %s", path)); } catch (Exception ex) { } } CartierSystemSettings systemSettings = systemSettingsMapper.selectValueByCode(ChromeDriverService.CHROME_DRIVER_WHITELISTED_IPS_PROPERTY); if (null != systemSettings) { System.setProperty(ChromeDriverService.CHROME_DRIVER_WHITELISTED_IPS_PROPERTY, systemSettings.getValue()); } logger.info("webdriver.chrome.driver={}", path); System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, path); ChromeOptions options = getChromeOptions(osName); driver = new ChromeDriver(options); } /** * 對非mac & Windows系統開啓隱藏頭模式 * @param osName * @return */ public static ChromeOptions getChromeOptions(String osName) { ChromeOptions options = new ChromeOptions(); if (!osName.startsWith("mac") && !osName.startsWith("windows")) { options.addArguments("--headless"); options.addArguments("--disable-gpu"); options.addArguments("--no-sandbox"); } options.addArguments("--verbose"); options.addArguments("window-size=1920x1920"); return options; }