takeScreenshot method from AShot library not working to capture screen shot for specific element

474 Views Asked by At

AutoTestWebDriver driver=new AutoTestWebDriver(); Screen shot for full page works. This is my code:

WebElement element = driver.findElement(By.id("element"));
    Screenshot shotFullscreen = new AShot().takeScreenshot(driver);// works fine
    Screenshot shot = new AShot().takeScreenshot(driver,element);
this is the error I get:

org.openqa.selenium.WebDriverException: unknown error: $ is not defined

What can be the issue?

2

There are 2 best solutions below

1
On

I solved it like this: Screenshot screenshot = new AShot().coordsProvider(new WebDriverCoordsProvider()).takeScreenshot(driver,driver.findElement(By.xpath(webElementXpath)));

(You need to specify which implementation to use -the WebDriverCoordsProvider or the JQuery one).

0
On

I encountered this kind of problem when im trying to screenshot a certain element on a page specifically the site's logo, luckily I already solved it by using WebDriverCoordsProvider().

Screenshot logoImageScreenShot = new AShot().coordsProvider(new 
WebDriverCoordsProvider()).takeScreenshot(driver, element);
ImageIO.write(logoImageScreenShot.getImage(), "PNG",new File("{Directory}\\logo.png"));
File savedLogo = new File("{Directory}\\logo.png");
    
if(savedLogo.exists()) {
   System.out.println("Image File Captured");
}
else {
   System.out.println("Image File Not Exist");
}

If you're using maven and encountered an error when running "mvn test", I advise to run first the "mvn clean" then "mvn test".