Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

page.waitForFunction() Evaluation failed: EvalError: Refused to evaluate a string as JavaScript #7395

Closed
uliss3s opened this issue Jun 28, 2021 · 7 comments

Comments

@uliss3s
Copy link

uliss3s commented Jun 28, 2021

Calling the code bellow:
page.waitForFunction("selector => document.querySelectorAll(selector).length === 0", "div[aria-label='Loading timeline']");

results in the error:

Exception in thread "main" com.microsoft.playwright.PlaywrightException: Error {
  message='Evaluation failed: EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self' 'unsafe-inline' https://*.twimg.com   https://www.google-analytics.com https://twitter.com https://app.link https://apis.google.com/js/platform.js https://appleid.cdn-apple.com/appleauth/static/jsapi/appleid/1/en_US/appleid.auth.js  'nonce-OTFjOTIzODItM2ViZi00NDQ0LWFhYWYtMTZhZjUwN2FjNjA5'".

    at eval (<anonymous>)
    at predicate (eval at evaluate (:303:29), <anonymous>:3:35)
    at eval (eval at evaluate (:303:29), <anonymous>:18:78)
    at onRaf (<anonymous>:1667:37)

Alternatively, i can code the logic like this and it works:

Object pageLoaded = Boolean.FALSE;
while (pageLoaded == Boolean.FALSE) {
    pageLoaded = page.evaluate("selector => (document.querySelectorAll(selector).length === 0)",
            "div[aria-label='Loading timeline']");
    customWait(500);
}

but i still need to know why the first function does not work.

@mxschmitt
Copy link
Member

mxschmitt commented Jun 30, 2021

Is your site publicly available? We would be happy to take a look then. It seems like like they have CSP headers set which prevent you from executing JavaScript functions using evaluate.

You can set bypassCSP when creating the context to fix it: https://playwright.dev/docs/api/class-browser#browser-new-context-option-bypass-csp

@mxschmitt mxschmitt transferred this issue from microsoft/playwright-java Jun 30, 2021
@mxschmitt
Copy link
Member

Closing for now since the issue seemed stale. Please reopen with a minimum reproducible.

@mnmkng
Copy link

mnmkng commented Sep 20, 2021

Repro for failure @mxschmitt

Playwright: 1.14.1
OS: Mac OS 11.5.2

const playwright = require('playwright');

const browser = playwright.chromium.launch()
    .then(async (browser) => {
        const page = await browser.newPage();
        await page.goto('https://github.com/topics/javascript');
        const bool = await page.waitForFunction(() => {
            const repoCards = document.querySelectorAll('article.border');
            return repoCards.length > 30;
        })
        console.log(bool);
        await browser.close();
    })

Interestingly, this works (replaced waitForFunction with evaluate, no other changes):

const playwright = require('playwright');

const browser = playwright.chromium.launch()
    .then(async (browser) => {
        const page = await browser.newPage();
        await page.goto('https://github.com/topics/javascript');
        const bool = await page.evaluate(() => {
            const repoCards = document.querySelectorAll('article.border');
            return repoCards.length > 30;
        })
        console.log(bool);
        await browser.close();
    })

@mnmkng
Copy link

mnmkng commented Nov 14, 2022

@mxschmitt Resurfacing this issue, as I ran into it again today. The reproduction above still works.

@remyoucef
Copy link

@mxschmitt I have this issue too.
@mnmkng Did you find a way around it?

@mnmkng
Copy link

mnmkng commented Apr 19, 2023

I used evaluate and implemented the waiting logic separately.

@remyoucef
Copy link

Thank you. I've done the same, and it's working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants