一、Linux机器安装google-chrome-stable
1、设置google-chrome软件源
sudo vim /etc/yum.repos.d/google-chrome.repo
[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
2、安装依赖[过程中如提示有其他依赖缺失,可至https://rpmfind.net/linux/rpm2html/search.php 查询对应名字rpm包 执行sudo yum install -y 安装即可]
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
3、安装google-chrome-stable
sudo yum install google-chrome-stable -y
二、准备客户端【webdriver随客户端发放到服务器】
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; }