Using SeleniumBase in CDP mode, both script executing methods generate an ‘Illegal return statement’ SyntaxError.
from seleniumbase import Driver
with Driver() as drv:
drv.init_cdp('https://example.org')
print(drv.cdp.eval_js('(() => "sample")()'))
How can such scripts be executed correctly?
hey, i had a similar issue. try using a vanilla function call like (function(){ return ‘sample’; })(); instead of the arrow func. seems cdp mode isnt cool with the es6 syntax sometimes.
Based on my own experience working with SeleniumBase in CDP mode, it appears that the JavaScript engine can be strict about using the modern arrow functions. I encountered a similar issue and found that reverting to the traditional function declaration style resolved the problem. Instead of using the arrow function, I used a self-invoking function such as (function() { return ‘sample’; })() which executed without errors. This indicates that the environment might not support ES6 syntax fully or handles it differently. Reviewing the browser compatibility notes might be beneficial as well.
I experienced a similar problem and found out that subtle environmental differences can sometimes cause CDP mode to misinterpret the syntax in unexpected ways. After a bit of trial and error, I realized that restructuring the script helped. Instead of using a concise form that some engines might not fully support, I rewrote my code by defining a function normally and invoking it immediately. During this process, careful analysis of console logs was really helpful, as it pointed to nuances in code structure that could trigger the error. In the end, paying closer attention to how the JavaScript is parsed by the underlying engine proved to be key.
hey, i fixed it by avoidng arrow funcs. i replaced it with a classic self-invoking function and it worked fine. seems like cdpmode causes issues with newer js syntax sometimes, so sticking with old school function style is the way to go.
From my investigations, I noticed that wrapping the code in an extra anonymous function before invoking it can help. In my case, I modified the script to define a helper function that returns the result of the arrow function execution. Calling this helper function ensured that the CDP mode receives a properly structured function body, which eliminates the syntax error. Although it might seem like an extra step, this approach provided a reliable workaround while testing with our internal projects and maintaining support for ES6 syntax where possible.