Syntax error with Evaluate JavaScript

Hi guys,

I’m trying to implement a simple Evaluate JavaScript code, but I keep getting a syntax error. here is the code:

${el}=    SeleniumLibrary.Get WebElement    ${LoginButton}
${actualText}=     Evaluate    JavaScript ${el} (elem) => elem.Value

Get WebElement successfully returns the element from provided locator.
Please see the attached screenshot for the error I’m getting.

Thank you in advance.

Keyword is called Execute Javascript. Also, you are referencing to SeleniumLibrary with the “Get Element” but posted this under “Browser” which is different library altogether…

Hi rasjani,

Sorry about the mistake with posting in “Browser” section. Fixed :slight_smile:

As for the Execute JavaScript, thanks for the tip. But I think it works differently from the Evaluate keyword I’m asking about? And even after replacing Evaluate with Execute JavaScript, I’m now getting different error.
Keyword failed: JavascriptException: Message: javascript error: Unexpected token '<

Evaluate keyword runs python code - there is no way to use that for evaluating javascript.

Second, you get error because you are essentially passing string <selenium.webdriver..blablabla> (python repr output) into javascript - it obviously fails as it is not js syntax. My js is a bit rusty but essentially your javascript looks like it has other issues too and without firing up proper selenium session, i’d suggest to try this;

Execute Javascript     return arguments[0].value     ARGUMENTS    ${el}

That is the way to pass any values from robot/python side to be used in the js side that runs in the browser. Each argument you place within the actual js code will just be treated as string but if you pass those as argument, they end up in arguments variable in js side with correct typing and thus, can be interacted properly on the js side…

1 Like

This is interesting. According of this user guide paragraph, Evaluate can be used with JavaScript? I even found some other examples of Evaluate JavaScript in this forum.

And here is a forum post, suggesting Evaluate JavaScript, to get text from an element…

  1. Your Initial code was Evaluate<insertmultiplespaceshere>Javascript. Multiple spaces mean a separator. And thus, your arguments ended up into this keyword.

  2. The post and docs you are referring to where about “robotframework-browserlibrary”, not “robotframework-seleniumlibrary”. You cant run both at the same time towards same browser instance and thus, cant mix and match keywords. If you are using Selenium, the keyword is Execute Javascript


Thank you for the clarification! Now it makes sense :slight_smile: The Execute JavaScript code you suggested, worked as expected.