ashot
ashot copied to clipboard
Full page screenshot
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
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!
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 and @maheshme12 If you are still facing the issue then trying out the slightly modified answer of @natalygoloborodko will work
-
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; }\"");
-
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