自己解決しました
Playwrightの関数を使うことで無事に解決することができました。
page.wait_for_function
を使うのが一番簡単なのですが、なぜか
playwright._impl._api_types.Error: 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'".
というエラーが発生してしまったので自分でPromiseを返す関数を作成して待機させました。
from playwright.sync_api import sync_playwright, Playwright, Response
code = """
async () => {
return await new Promise((resolve) => {
setInterval(() => {
if (!window.popupLock) {
resolve();
}
}, 1000);
});
}
"""
def main(playwright: Playwright):
def response_handler(response: Response):
page.evaluate(code)
page.evaluate("window.popupLock = true;")
#...競合する処理
page.evaluate("window.popupLock = false;")
browser = playwright.chromium.launch(headless=False)
page = browser.new_page()
page.goto("")
page.on("response", response_handler)
with sync_playwright() as playwright:
main(playwright)
お騒がせしました。誰かの役に立てることを願います。
Like!