berstend / puppeteer-extra

💯 Teach puppeteer new tricks through plugins.
https://extra.community
MIT License
6.42k stars 739 forks source link

[Feature] playwright-python by stealth.min.js #772

Open dly667 opened 1 year ago

dly667 commented 1 year ago

Can playwright-python use this library through page.evaluate(''stealth.min.js"), It didn't work when tested

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        page = await browser.new_page()
        # await stealth_async(page)
        with open('data/stealth/js/test.js') as f:
            js = f.read()
        # page = await context.new_page()
        rs = await page.evaluate(js)
        # rs = await page.evaluate('()=>{%s}' %js)
        # print(rs)
        # await context.add_init_script('()=>{%s}' % js)

        await page.goto('https://bot.sannysoft.com')
RG9 commented 1 year ago

It doesn't work, because some scripts require actual Playwright instance. For example: https://github.com/berstend/puppeteer-extra/blob/master/packages/puppeteer-extra-plugin-stealth/evasions/user-agent-override/index.js that require page:

  async onPageCreated(page) {
    // Determine the full user agent string, strip the "Headless" part
    let ua =
      this.opts.userAgent ||
      (await page.browser().userAgent()).replace('HeadlessChrome/', 'Chrome/')

    (...)

    const client =
      typeof page._client === 'function' ? page._client() : page._client
    client.send('Network.setUserAgentOverride', override)