How to capture screenshot of a WebElement within a webpage but not the entire screen or page through Selenium

前端 未结 3 1163
臣服心动
臣服心动 2020-12-04 04:11

I have to capture a screenshot of an image of a particular website. Maybe this is 20% off entire screen, I have used below code, it is capturing the entire screen. Which is

3条回答
  •  萌比男神i
    2020-12-04 04:26

    I am using selenium-java-3.141.59 and ChromeDriver 83.0.4103.39, this code below works perfectly for me:

        WebDriver driver = new ChromeDriver();
       driver.get("https://www.google.com/");
      WebElement element = driver.findElement(By.id("hplogo"));
      Screenshot screenshotHeader = new AShot().coordsProvider(new WebDriverCoordsProvider()).shootingStrategy(ShootingStrategies.viewportPasting(100)).takeScreenshot(driver, element);
        try {
            ImageIO.write(screenshotHeader.getImage(),"jpg",new File("C:/TESTSELENIUM/Google.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    

提交回复
热议问题