【问题标题】:Reducing screenshot taking time on selenium减少在 selenium 上的屏幕截图时间
【发布时间】:2023-04-08 11:29:01
【问题描述】:

如果代码在 localhost 的服务器上执行,截屏速度非常快,不到一秒(大约 400-500 毫秒):

private RemoteWebDriver driver;
private DesiredCapabilities dc = new DesiredCapabilities();

@Before
public void setUp() throws MalformedURLException {
    ....
    ....
    dc.setCapability(CapabilityType.BROWSER_NAME, BrowserType.CHROME);
    driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), dc);
}

@Test
public void test() throws InterruptedException, IOException {
    driver.get("https://google.com");
    driver.findElement(By.name("q")).sendKeys("automation test");
    
    long before = System.currentTimeMillis();
    //here is the problem
    File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    long after = System.currentTimeMillis();
    System.out.println(after-before);

    FileUtils.copyFile(srcFile, new File("/Users/name/localpath/test.png"));
}

但如果将目标服务器更改为安装了 Selenium 服务器的 public ip,截屏会更慢(大约 5 秒)。也许这可能是由于客户端和服务器之间的距离,所以一定有区别。

是否可以减少截屏时间?我正在考虑降低图像分辨率,但如何调整呢?

【问题讨论】:

  • 在此期间检查您的网络使用情况。很可能是下载图像所需的时间。
  • @chrylis-cautiouslyoptimistic- 除了网络问题,还有其他方法可以让这段代码更好地工作吗?
  • 您是否尝试过将屏幕截图设为 base64 (selenium.dev/selenium/docs/api/java/org/openqa/selenium/…)。你也可以看看这个答案:stackoverflow.com/a/42143900/1387701
  • 这个文件是存放在远程机器上,复制到本地系统吗?
  • @PDHide 从客户端截屏,直接从服务器复制。不先存储在服务器上。

标签: java selenium selenium-webdriver remotewebdriver takesscreenshot


【解决方案1】:

裁剪屏幕截图

获得更小尺寸屏幕截图(除了使用各种文件格式)的一种方法是更改​​屏幕截图的大小:您可以截取您特别感兴趣的 Web 元素(或页面区域)的屏幕截图.

尝试以下操作(您需要使用BufferedImage class):

@Test
public void test() throws InterruptedException, IOException {
    driver.get("https://google.com");
    driver.findElement(By.name("q")).sendKeys("automation test");
    
    long before = System.currentTimeMillis();
    File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

    Point p = element.getLocation();
    int width = element.getSize().getWidth();  
    int height = element.getSize().getHeight();
    BufferedImage img = ImageIO.read(scrFile);
    BufferedImage elementScreenshot = img.getSubimage(p.getX(), p.getY(), width, height);
    //NOTE: the line above will crop the full page screenshot to element dimensions, change width and height if you wish to crop to region
    Image.write(elementScreenshot, "png", scrFile);

    FileUtils.copyFile(srcFile, new File("/Users/name/localpath/test.png"));
    long after = System.currentTimeMillis();
    System.out.println(after-before);
}

【讨论】:

  • 感谢您的回答,但这似乎并不能解决问题。实际上问题出在File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); 行,而不是之后。抱歉,问题中的代码已更新。
  • 哦,我明白了。有一个名为 Ashot 的 Java 库可以对网页的不同部分进行屏幕截图。也许你可以试试。见stackoverflow.com/questions/52015559/…
【解决方案2】:

问题很可能是通过网络传输文件。 IT 也可能在创建文件。

我建议尝试使用 Base64 输出,看看是否会减少传输时间:

String screenshotAsBase64String = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BASE64);
        

【讨论】:

  • 感谢您的回复,我试过了,但这并没有减少时间。
  • 我认为就原生硒而言,您不走运。尽管屏幕截图行为因浏览器而异。
猜你喜欢
  • 2016-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-13
相关资源
最近更新 更多