Xewdy444 / Playwright-reCAPTCHA

A Python library for solving reCAPTCHA v2 and v3 with Playwright
https://pypi.org/project/playwright-recaptcha/
MIT License
240 stars 33 forks source link

Idea #107

Closed populated closed 2 months ago

populated commented 2 months ago

No clue if this is possible. I mean, it was logical enough to work with a Cloudflare turnstile solver. Couldn't you use the reCaptcha challenge URI (given by a user) and build a separate page (local) with just the captcha? As of now, this is limited to pages where the captcha is visible without needing to do other actions. What I'm suggesting would, I mean, logically erase this limitation, as you could solve the captcha without requiring the other actions.

Xewdy444 commented 2 months ago

You can do something like this:

from playwright.sync_api import sync_playwright

from playwright_recaptcha import recaptchav2

RECAPTCHA_HTML = """
<!DOCTYPE html>
<html>
    <head>
        <script src="https://www.google.com/recaptcha/api.js" async
            defer></script>
    </head>
    <body>
        <div
            class="g-recaptcha"
            data-sitekey="{sitekey}"></div>
    </body>
</html>
"""

def main() -> None:
    with sync_playwright() as playwright:
        browser = playwright.firefox.launch()
        page = browser.new_page()
        page.goto("https://www.google.com")

        page.set_content(
            RECAPTCHA_HTML.format(sitekey="6Le-wvkSAAAAAPBMRTvw0Q4Muexq9bi0DJwx_mJ-")
        )

        with recaptchav2.SyncSolver(page) as solver:
            token = solver.solve_recaptcha(wait=True)
            print(token)

if __name__ == "__main__":
    main()

The one requirement is that you load a website with the page before setting the reCAPTCHA HTML. If you don't, you'll get this error: image

Xewdy444 commented 2 months ago

Added an example for this