gpt4 book ai didi

java - 在 Chrome 浏览器中使用 selenium webdriver 截取屏幕截图

转载 作者:行者123 更新时间:2023-12-02 02:54:03 25 4
gpt4 key购买 nike

我正在学习 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);
}
}

最佳答案

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")

请记住在方法中更改文件目标。

关于java - 在 Chrome 浏览器中使用 selenium webdriver 截取屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43386398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com