我正在学习selenium Webdriver。我试图在chrome浏览器上截取屏幕截图,但是我对下面的代码有异常(注意:同样的代码片段适用于firefox)。请帮我在Chrome上截取屏幕截图,请有人解释为什么以下代码无法在Chrome上运行。
public class ScreenShot
{
public static void main(String[] args) throws IOException
{
String key = "webdriver.chrome.driver";
String value = "./driver/chromedriver.exe";
System.setProperty(key, value);
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.co.in");
TakesScreenshot screen = (TakesScreenshot) driver;
File srcFile = screen.getScreenshotAs(OutputType.FILE);
File destFile = new File("d:/google.png");
FileUtils.copyFile(srcFile, destFile);
}
}
答案 0 :(得分:2)
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
public static String captureScreenshot (WebDriver driver, String screenshotName){
try {
TakesScreenshot ts = (TakesScreenshot)driver;
File source = ts.getScreenshotAs(OutputType.FILE);
String dest = "/Users/CD6255ABQA/Desktop/Debug Images/" + screenshotName + ".png";
File destination = new File(dest);
FileUtils.copyFile(source, destination);
return dest;
}
catch (IOException e) {return e.getMessage();}
}
使用
调用它String screenpath = captureScreenshot(driver, "ScreenshotName")
请记住在方法中更改文件目的地。