Pass an Xpath in Execute Javascript

Hello all,

Juste a question on execute javascript… In my old old old version of Rf+SeleniumLibrary, i can write a command with an Xpath like this :

  • Execute Javascript var Map = $(“[class=‘widget-container’]”);

And it s works

With my new version RF+Selenium Library, the same command give to me

Executing JavaScript:
var Carto = $(“[class=‘widget-container’]”);
Without any arguments.
20250320 14:26:19.249 : FAIL :
JavascriptException: Message: ReferenceError: $ is not defined

I try to escape the $ but nothing to do…

Anyone have a idea about that ?

Thank u so

Fabrice

Noting that error message I think it is saying that $ is a non existent variable and thus not defined [Ref]. The $ is commonly used by jQuery as a shortcut to the jQuery namespace but not exclusively and this is important. The jQuery docs talk about that here. So depending on your application it my simply not be defined. I am guessing your application changed and it is not an issue with SeleniumLibrary nor Robot Framework in interpreting that execute JavaScript call.

One way to verify this is to open the DevTools within your browser, navigate to the page you call this on and then pasting this code into the console tab. (Note you may also need to get the page into a similar state if there is some other previous actions). But I would expect to see the same error message.

Also re-reading this I know that $x("//some/xpath/locator"); in the console window gives you an element so should that be $x(..) and not $(..) - noting the missing x? I have never tried to use that syntax within a Execute JavaScript call nor outside of the console window and I thought that was only a DevTool/“within the console window” type function.

One could also have a discussion about getting elements, locators and why this method but will save that discussion for later ..

hello EdManlove,

Yes, the syntax with $x is different, you can use a real xpath in the browser console, but in execute javascript, it is the same error

Executing JavaScript:
var Carto = $x(“//*[@class=‘widget-container’]”);
Without any arguments.
20250320 15:14:39.802 : FAIL :
JavascriptException: Message: ReferenceError: $x is not defined

I think that the $ is consider like an robotframework variable, and it is not the case

The reason to do that is to set the element in my JS var= to be able to work a mouse wheel or others JS possibilities by examples

Thxs

Fabrice

The error message itself - how it is formatted and what is says,

JavascriptException: Message: ReferenceError: $x is not defined

to me seems to indicate that Robot is not, either corrupting it or interpreting it before it is sent. That does look like a properly formatted JavaScript Error that is getting returned from the JavaScript call. One could verify by setting the log level to TRACE and seeing what JavaScript is sent to and from. This might be the first debugging step I would do/suggest ..