简体   繁体   中英

How to take a picture of a WebElement on Android?

I want to be able to take pictures of elements on Android, for example on a page with 4 canvases I want to get a screen grab of only the third canvas. I will then do something on the page and use sikuli to make sure the canvas hasn't changed. I can do this on the desktop with the following code:

public void shootWebElement(WebElement element, AndroidDriver driver,String fileName) throws IOException  {

    File screen = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    Point p = element.getLocation();
    int width = element.getSize().getWidth();
    int height = element.getSize().getHeight();

    BufferedImage img = ImageIO.read(screen);
    BufferedImage dest = img.getSubimage(p.getX(), p.getY(), width,   
                         height);

    ImageIO.write(dest, "png", screen);
    File f = new File(fileName);
    FileUtils.copyFile(screen, f);
}

This code runs on Android but the picture i get back isn't of the element, it's a partial picture of the entire view (this picture will be the same for any element I use as a parameter).

Does anyone know a way around this?

使用selendroid,不推荐使用AndroidDriver,并且不再对其进行维护。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM