Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
Reducir el tiempo de toma de capturas de pantalla en Selenium

Tomar una captura de pantalla es muy rápido si el código se ejecuta en un servidor en localhost , menos de un segundo (alrededor de 400-500 ms):

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

Pero si el servidor de destino se cambia a public ip que tiene instalado el servidor Selenium, tomar la captura de pantalla será más lento (alrededor de 5 segundos). Tal vez esto podría deberse a la distancia entre el cliente y el servidor, por lo que debe haber una diferencia.

¿Es posible reducir el tiempo de captura de pantalla? Estoy pensando en reducir la resolución de la imagen, pero ¿cómo la ajusto?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

recortar la captura de pantalla

Una forma de obtener capturas de pantalla de menor tamaño (aparte de usar varios formatos de archivo) es cambiar el tamaño de la captura de pantalla: puede tomar una captura de pantalla de un elemento web (o una región de una página) que le interese especialmente.

Intente lo siguiente (necesitará usar la clase BufferedImage ):

 @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); }
over 4 years ago · Santiago Trujillo Report

0

Lo más probable es que el problema sea transferir el archivo por cable. También podría estar en la creación del archivo.

Sugeriría probar con la salida Base64 para ver si eso reduce el tiempo de transferencia:

 String screenshotAsBase64String = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BASE64);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!