Maven
<!--自动化测试截图-->
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.14.0</version>
</dependency>
package net.cnki.utils;
/**
* @author SongBin on 2019/3/19.
*/
/**
* @author SongBin on 2019/3/19.
*/
import net.cnki.config.ChromeDriverConf;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
public class AutoTestUtil {
private static final Logger log = LoggerFactory.getLogger(AutoTestUtil.class);
public static String getScreenshot(String url) throws InterruptedException{
WebDriver driver = null;
String savePath ="";
try {
String driverPath = ChromeDriverConf.driverPath;
String picSavePath = ChromeDriverConf.picSavePath;
String chromePath = ChromeDriverConf.chromePath;
savePath = picSavePath + UUID.randomUUID().toString() + ".png";
System.setProperty("webdriver.chrome.driver", driverPath);
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary(chromePath);
// 设置为 headless 模式 (必须)
chromeOptions.addArguments("--headless");
// 设置浏览器窗口打开大小 (非必须)
chromeOptions.addArguments("--window-size=1920,1080");
// -–no-sandbox”参数是让Chrome在root权限下跑
chromeOptions.addArguments("--no-sandbox");
//设置窗口最大化
// chromeOptions.addArguments("--start-maximized");
driver = new ChromeDriver(chromeOptions);
//System.setProperty("webdriver.gecko.driver", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
// WebDriver driver = new FirefoxDriver();
//设置窗口最大化
// driver.manage().window().maximize();
String title = driver.getTitle();
System.out.println(title);
driver.get(url);
// 与浏览器同步非常重要,必须等待浏览器加载完毕
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//暂停五秒钟后截图
Thread.sleep(8000);
//截图到output
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
// String savePath = "D:\\imgupload\\screenshot.png";
//复制内容到指定文件中
FileUtils.copyFile(scrFile, new File(savePath));
} catch (IOException e) {
e.printStackTrace();
}
}catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
}finally {
driver.quit();
}
return savePath;
}
public static void main(String[] args) throws InterruptedException {
String url ="http://127.dev.cnki.net:8081/qa?keyword=组组通项目情况";
getScreenshot(url);
}
}
package net.cnki.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @author SongBin on 2019/3/19.
*/
@Component
public class ChromeDriverConf {
public static String driverPath;
public static String picSavePath;
public static String chromePath;
@Value("${chromedriver.chromePath}")
public void setChromePath(String chromePath) {
this.chromePath = chromePath;
}
@Value("${chromedriver.driverPath}")
public void setDriverPath(String driverPath) {
this.driverPath = driverPath;
}
@Value("${chromedriver.picSavePath}")
public void setPicSavePath(String picSavePath) {
this.picSavePath = picSavePath;
}
}
application.yml配置:
server:
port: 8055
chromedriver:
driverPath: C:/softs/chromedriver.exe
chromePath: C:/softs/chrome/chrome.exe
picSavePath: D:/imgupload/
问题解决:
1.NoSuchMethodError for com.google.common.base.Preconditions.checkState
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.4</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
2.在centos部署时可能会出现的问题,杜绝方法:
1.chrome和chromedriver版本对应
使用Selenium时,如何选择ChromeDriver驱动版本对应Chrome浏览器版本
2.root权限打开chrome浏览器
需要加配置项
// -–no-sandbox”参数是让Chrome在root权限下跑
chromeOptions.addArguments("--no-sandbox");
3.同时设置chrome和chromedriver的地址:
System.setProperty("webdriver.chrome.driver", driverPath);
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary(chromePath);
更多内容请访问:IT源点
注意:本文归作者所有,未经作者允许,不得转载