【发布时间】: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