首页 > 解决方案 > 无法使用 selenium webdriver 完全剪切宽横幅图像,仅剪切图像的一部分

问题描述

我正在尝试使用 selenium webdriver java 从 iframe 中的网页中剪切宽横幅图像,但使用以下代码剪切后我只能获得部分横幅图像。

我试过下面的代码 wghich 只给了我部分横幅图像。

 WebElement element = driver.findElement(By.xpath("//amp- 
 img[@class='img_ad 
 i-amphtml-layout-fixed i-amphtml-layout-size-defined i-amphtml-element 
 i-amphtml-layout']//img[@class='i-amphtml-fill-content i-amphtml- 
 replaced-content']"));
 File screen = ((TakesScreenshot) 
 driver).getScreenshotAs(OutputType.FILE);
 Point point = element.getLocation();
 int xcordinate = point.getX();
 int ycordinate = point.getY();
 int imageWidth = element.getSize().getWidth();
 int imageHeight = element.getSize().getHeight();
 BufferedImage img = ImageIO.read(screen);
 BufferedImage destination = img.getSubimage(xcordinate, ycordinate, 
 imageWidth, imageHeight);
 ImageIO.write(destination, "png", screen);
 File screenshotLocation = new 
 File("C:\\shots\\screenshotOfElement.png");
 FileUtils.copyFile(screen,screenshotLocation);

请让我知道我应该在我的代码中进行哪些更改以在使用上述代码剪切后获得完整的横幅图像。

标签: javaseleniumwebdriverscreenshot

解决方案


推荐阅读