Reputation: 510
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!
Upvotes: 1
Views: 2477
Reputation: 457
JavascriptExecutor jse = (JavascriptExecutor) driver;
Object imageURL = jse.executeScript("return arguments[0].toDataURL('image/png').substring(22);",canvas);
With this code you get the string of base64 Image
Upvotes: 0
Reputation: 25596
For your original code, you just need to add return
to get a value back. You can also cast the Object
return as string, if you want.
String imageURL = (String) jse.executeScript("return arguments[0].toDataURL('image/png');", canvas);
System.out.println(imageURL);
For the width portion, you don't even need JSE.
WebElement canvas = driver.findElement(...);
System.out.println(canvas.getAttribute("width"));
Upvotes: 3
Reputation: 2958
Add return
keyword to your script.
PS: let me know if it's resolved, else we can try something else.
Upvotes: 4