ashot icon indicating copy to clipboard operation
ashot copied to clipboard

Full page screenshot

Open maheshme12 opened this issue 6 years ago • 3 comments

I tried to get full page screenshot, I am able to do that but if the page has fixed header, the header is getting repeated. I know we scroll and take a screenshot and append to one image but can we get a full page screenshot without repetitive headers. The code I am using is:

` static void takeScreenShot() throws InterruptedException, IOException {

    System.setProperty("webdriver.chrome.driver",
            "/PATH_TO_DRIVER/chromedriver");
    WebDriver driver = new ChromeDriver();
    driver.manage().window().fullscreen();
    driver.get("https://apple.com");
    driver.manage().timeouts().implicitlyWait(2, TimeUnit.MINUTES);
    ViewportPastingStrategy vpxs = new ViewportPastingStrategy(500);
    final Screenshot screenshot = new AShot().shootingStrategy(vpxs).takeScreenshot(driver);
    final BufferedImage image = screenshot.getImage();
    ImageIO.write(image, "PNG", new File(
            "/SAVE_LOCATION/"
                    + "a.png"));
    driver.quit();
}

`

and screenshot is imagewithheader

maheshme12 avatar Jun 07 '18 13:06 maheshme12

Well, in my case AShot couldn't hide/ignore/cur off the header. But I foud the solution by using the JavascriptExecutor.

If header is fixed only by css styles i add this code to your test after opening the required page: JavascriptExecutor js = (JavascriptExecutor) driver; WebElement header = driver.findElement(By.xpath("xpath-of-your-element")); js.executeScript("arguments[0].setAttribute('style', 'position: static !important;')",header);

If your header is fixed by some javascript, then use this code in your test just after opening the required page: JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("document.head.appendChild(document.createElement(\"style\")).innerHTML = \"css-locator-of-your-element {position: static !important; }\"");

First code uses xpath locator for header. Second code uses css locator for header.

Good luck!

natalygoloborodko avatar Nov 15 '18 12:11 natalygoloborodko

Am too facing this issue, but i can't make above javascript executor logic. As my utility will capture and compare diffrent webpage every time. Any leads will be highly appreciated as i have already spent many hours and still clueless to handle this.

Anoopkumars avatar Jun 30 '20 14:06 Anoopkumars

@Anoopkumars and @maheshme12 If you are still facing the issue then trying out the slightly modified answer of @natalygoloborodko will work

  1. If the header is not the point of interest which has to be captured, then simply hide the header through css and capture the page JavascriptExecutor js = (JavascriptExecutor) webDriver; js.executeScript("document.head.appendChild(document.createElement(\"style\")).innerHTML = \"css-locator-of-your-element-to-be-hidden {visibility:hidden; }\"");

  2. If the header is important and it is required to be captured in the screenshot then, keeping the position relative will do the magic JavascriptExecutor js = (JavascriptExecutor) webDriver; js.executeScript("document.head.appendChild(document.createElement(\"style\")).innerHTML = \"css-locator-of-your-element-to-be-made-relative {position: relative; }\"");

Hope this helps your problem

suneel944 avatar Dec 20 '20 21:12 suneel944