user3364652 user3364652 - 25 days ago 16
HTML Question

How to get the URL of an image inside a canvas element with Selenium in Java, and how to use JavascriptExecutor?

My specific goal is to get the image URL from a canvas container, here is what is did:

JavascriptExecutor jse = (JavascriptExecutor) driver;

Object imageURL = jse.executeScript("arguments[0].toDataURL('image/png');",canvas);


I'm getting a return value of null.

Then I've tried to do something more basic, like getting the width attribute of the canvas.

JavascriptExecutor jse = (JavascriptExecutor) driver;
Object width= jse.executeScript("arguments[0].getAttribute('width');",canvas);


Again I'm getting null. The canvas
WebElement
is well identified by Selenium and it's "width" attribute is exists - I can retrieve it with
WebDriver
's
getAttribute
method.

I guess I'm using it wrong.

Thanks for the help!

Answer

Add return keyword to your script.

PS: let me know if it's resolved, else we can try something else.

Comments