gpt4 book ai didi

java - 使用 Selenium 截屏

转载 作者:行者123 更新时间:2023-12-01 09:00:55 27 4
gpt4 key购买 nike

我从另一个包导入这个类并尝试调用这个方法,但它不起作用。

当我在同一个类中创建此方法并调用它时,它正在工作。

 private void getScreenshot() throws IOException
{
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
SimpleDateFormat dateFormat = new SimpleDateFormat("DD-MM-YYYY/hh-mm-ssaa");
String destfile = dateFormat.format(new Date()) + ".png";
FileUtils.copyFile(scrFile, new File("D:\\workspace\\Firewall\\Resources\\ScreenShots\\"+destfile));
}

最佳答案

我猜主要原因是你导入了错误的库。查看:

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

如果您的库相同,请尝试使用我的方法:

public class TakeScreenshot {
WebDriver driver;
public TakeScreenshot(WebDriver driver){
this.driver = driver;
}
public void ScreenShot(String nameTc)
{
// Take screenshot and store as a file format
File src= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
// now copy the screenshot to desired location using copyFile //method
FileUtils.copyFile(src, new File("bin/" + nameTc + ".png"));
}
catch (IOException e)
{
System.out.println(e.getMessage());
}} }

关于java - 使用 Selenium 截屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41672961/

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