# I can not do and save a screenshot to a WebElement (image) using AShot

**URL:** <https://forum.katalon.com/t/i-can-not-do-and-save-a-screenshot-to-a-webelement-image-using-ashot/31330>\
**Category:** Web Testing\
**Tags:** web-testing, katalon-studio\
**Created:** [August 10, 2019, 6:16am UTC](https://forum.katalon.com/t/i-can-not-do-and-save-a-screenshot-to-a-webelement-image-using-ashot/31330 "2019-08-10T06:16:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Valeria\_Ciobanu](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/valeria_ciobanu/32/7020_2.png) [@Valeria\_Ciobanu](https://forum.katalon.com/u/Valeria_Ciobanu)\
**Post date:** [August 10, 2019, 6:16am UTC](https://forum.katalon.com/t/i-can-not-do-and-save-a-screenshot-to-a-webelement-image-using-ashot/31330/1 "2019-08-10T06:16:38Z")

</div>

Hello!  
On the page I have a picture. I need to save this image. For this I decided to use the method of creating the Ahot screenshots of a webelement.

—\>For this we created the keyword.

public class takeScreenshot {

```
@Keyword

public void takeScreen(TestObject object){
	WebElement element = WebUiCommonHelper.findWebElement(object,20)
	WebDriver driver = DriverFactory.getWebDriver();	
	
	Screenshot screenshot = new AShot().takeScreenshot(driver,element);

	ImageIO.write(screenshot.getImage(),"PNG",new File("path\\to\\my\\directory\\image.png"))
}

```

}

–=\>My test:

WebUI.openBrowser(‘URL’)

WebUI.maximizeWindow()

WebUI.scrollToElement(findTestObject(‘img\_code’), 3)  
CustomKeywords.‘screenshots.takeScreenshot.takeScreen’(findTestObject(‘img\_code’))

—\>And I get the error

java.lang.RuntimeException: org.openqa.selenium.JavascriptException: javascript error: $ is not defined  
(Session info: chrome=76.0.3809.100)  
Build info: version: ‘3.141.59’, revision: ‘e82be7d358’, time: ‘2018-11-14T08:25:53’  
System info: host: ‘DESKTOP-PS8G0QM’, ip: ‘192.168.56.1’, os.name: ‘Windows 10’, os.arch: ‘amd64’, os.version: ‘10.0’, java.version: ‘1.8.0\_181’  
Driver info: com.kms.katalon.selenium.driver.CChromeDriver  
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 76.0.3809.100, chrome: {chromedriverVersion: 76.0.3809.68 (420c9498db8ce…, userDataDir: C:\Users\Admin\AppData\Loca…}, goog:chromeOptions: {debuggerAddress: localhost:49678}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}  
Session ID: 5821b158f744f8b9b07cc1ffb2f04e90  
at ru.yandex.qatools.ashot.util.InnerScript.execute(InnerScript.java:32)  
at ru.yandex.qatools.ashot.util.JsCoords.findCoordsWithJquery(JsCoords.java:24)  
at ru.yandex.qatools.ashot.coordinates.JqueryCoordsProvider.ofElement(JqueryCoordsProvider.java:14)  
at ru.yandex.qatools.ashot.coordinates.CoordsProvider.ofElements(CoordsProvider.java:21)  
at ru.yandex.qatools.ashot.AShot.takeScreenshot(AShot.java:116)  
at ru.yandex.qatools.ashot.AShot.takeScreenshot(AShot.java:134)  
at ru.yandex.qatools.ashot.AShot$takeScreenshot.call(Unknown Source)  
at screenshots.takeScreenshot.takeScreen(takeScreenshot.groovy:52)  
at screenshots.takeScreenshot.invokeMethod(takeScreenshot.groovy)  
at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:50)  
at download\_images.run(download\_images:34)  
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)  
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)  
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)  
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)  
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)  
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)  
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)  
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)  
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)  
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)  
at TempTestCase1565416962945.run(TempTestCase1565416962945.groovy:21)  
Caused by: org.openqa.selenium.JavascriptException: javascript error: $ is not defined  
(Session info: chrome=76.0.3809.100)  
Build info: version: ‘3.141.59’, revision: ‘e82be7d358’, time: ‘2018-11-14T08:25:53’  
System info: host: ‘DESKTOP-PS8G0QM’, ip: ‘192.168.56.1’, os.name: ‘Windows 10’, os.arch: ‘amd64’, os.version: ‘10.0’, java.version: ‘1.8.0\_181’  
Driver info: com.kms.katalon.selenium.driver.CChromeDriver  
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 76.0.3809.100, chrome: {chromedriverVersion: 76.0.3809.68 (420c9498db8ce…, userDataDir: C:\Users\Admin\AppData\Loca…}, goog:chromeOptions: {debuggerAddress: localhost:49678}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}  
Session ID: 5821b158f744f8b9b07cc1ffb2f04e90  
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)  
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)  
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)  
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)  
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)  
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)  
at com.kms.katalon.selenium.driver.CChromeDriver.execute(CChromeDriver.java:20)  
at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:489)  
at ru.yandex.qatools.ashot.util.InnerScript.execute(InnerScript.java:30)  
… 21 more

—\>Please tell me what I can do?

---

<div class="post-metadata">

**Author:** ![Arnel](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/arnel/32/6360_2.png) [@Arnel](https://forum.katalon.com/u/Arnel)\
**Post date:** [August 10, 2019, 2:59pm UTC](https://forum.katalon.com/t/i-can-not-do-and-save-a-screenshot-to-a-webelement-image-using-ashot/31330/2 "2019-08-10T14:59:20Z")

</div>

Hi Valeria,

I’m quite intrigued about this… Are you sure that this error message you have sent is related to the code you shared?? Because as you can see in the error message it finds **‘$’**

> [@Valeria\_Ciobanu](#):
>
> java.lang.RuntimeException: org.openqa.selenium.JavascriptException: javascript error: $ is not defined

Do you have that character in your code?? Maybe that error happens before it hits your screenshot code. Try to check…

Also, based on the link below. . . . I’m thinking that you have a ‘js.executescript()’ that doesn’t functioning well and that character ‘$’ is written inside your executeScript parameter.

> <https://stackoverflow.com/questions/39951120/getting-is-not-defined>

> <https://stackoverflow.com/questions/28670805/how-fix-selenium-webriver-error-is-not-defined>

I did some research about this and you have the same issue with the link below:

> <https://github.com/pazone/ashot/issues/68>
>
> Hii Ashot Team,
> I'm getting WebDriver Exception for taking screenshot of an invi…sible element (need to scroll down to find this element).
> \*\*Exception:\*\*
> Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: $ is not defined
> \*\*Code :\*\*
> WebElement element = driver.findElement(By.cssSelector("#imgId"));
> Screenshot screenshot = new AShot().shootingStrategy(new ViewportPastingStrategy(1000)).takeScreenshot(driver,element);
> ImageIO.write(screenshot.getImage(), "PNG", new File("C:\\screenshot.png"));

There are some replies there that might help you…

Hope that helps. . . 🙂

---

<div class="post-metadata">

**Author:** ![albert.vu](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/albert.vu/32/20595_2.png) [@albert.vu](https://forum.katalon.com/u/albert.vu)\
**Post date:** [August 8, 2022, 7:42am UTC](https://forum.katalon.com/t/i-can-not-do-and-save-a-screenshot-to-a-webelement-image-using-ashot/31330/3 "2022-08-08T07:42:58Z")

</div>


