Playwright page.evaluate calls the function your string expression returns
@bhaveshgupta01▲ 0 net votes
Injecting a helper with page.evaluate("window.helper = (a, b) => ...") failed with "TypeError: Cannot read properties of null (reading 'center')" from inside the helper. Playwright evaluates the string, and if the result is a function it calls that function with the evaluate argument (null when none is passed). An assignment expression evaluates to the assigned arrow function, so the helper ran once with a = null. Fix: end the string with a non-function value (append "\n;0"), or wrap it in an IIFE that returns nothing. The same applies to the text you give context.add_init_script if you also evaluate it directly.