@@ -306,41 +306,50 @@ class PageWithStickyNavMixin:
306306 def scrollToRevealElement (self , selector = None , xpath = None , stuckToBottom = True ):
307307 module_path = '/assets/javascript/esm/stick-to-window-when-scrolling.mjs'
308308 js_module_var = "stickAtBottomWhenScrolling" if stuckToBottom else "stickAtTopWhenScrolling"
309+
310+ # We pass the locator and the type as arguments to avoid quote collisions
311+ locator = selector if selector else xpath
312+ locator_type = 'selector' if selector else 'xpath'
309313
310- element_finder = (
311- f"document.querySelector('{ selector } ')" if selector
312- else f"document.evaluate('{ xpath } ', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue"
313- )
314-
315- # Check if the module is already initialized in window, if not, import it
316- # Then execute the specific handler
317- js_async_code = f"""
314+ js_async_code = """
318315 var callback = arguments[arguments.length - 1];
319-
320- async function run() {{
321- try {{
322- // Initialize module on window if not already present
323- if (!window.__stickyNavModule) {{
324- window.__stickyNavModule = await import('{ module_path } ');
325- }}
316+ var locator = arguments[0];
317+ var type = arguments[1];
318+ var moduleVar = arguments[2];
319+ var modulePath = arguments[3];
320+
321+ async function run() {
322+ try {
323+ if (!window.__stickyNavModule) {
324+ window.__stickyNavModule = await import(modulePath);
325+ }
326326
327- const handler = window.__stickyNavModule. { js_module_var } ;
328- const element = { element_finder } ;
327+ const handler = window.__stickyNavModule[moduleVar] ;
328+ let element;
329329
330- if (element && handler) {{
330+ if (type === 'selector') {
331+ element = document.querySelector(locator);
332+ } else {
333+ element = document.evaluate(locator, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
334+ }
335+
336+ if (element && handler) {
331337 handler.scrollToRevealElement(element);
332338 callback(true);
333- }} else { {
339+ } else {
334340 callback(false);
335- }}
336- }} catch (e) { {
341+ }
342+ } catch (e) {
337343 callback('Error: ' + e.message);
338- }}
339- }}
344+ }
345+ }
340346 run();
341347 """
342348
343- result = self .driver .execute_async_script (js_async_code )
349+ # Pass the variables separately. Selenium serializes them correctly.
350+ result = self .driver .execute_async_script (
351+ js_async_code , locator , locator_type , js_module_var , module_path
352+ )
344353
345354 if result is not True :
346355 raise Exception (f"Failed to scroll to element: { result } " )
0 commit comments