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

How to solve when the reCaptcha (v2) is within an iFrame? #69

Closed JackYouk closed 5 months ago

JackYouk commented 8 months ago

Getting "playwright_recaptcha.errors.RecaptchaNotFoundError: The reCAPTCHA was not found." due to the reCaptcha being inside of an iFrame.

Tried to solve this issue by passing the iframe context instead of the page context into the SyncSolver function such that

captcha_iframe = await page.query_selector('iframe[title="reCAPTCHA"]')
if captcha_iframe:
     frame = await captcha_iframe.content_frame()
     recaptchav2.SyncSolver(page=frame, capsolver_api_key="CAP-123") as solver:
          token = solver.solve_recaptcha(wait=True, image_challenge=True)
          print(token)

but run into the error 'Permission denied to access property "docShell" on cross-origin object'.

Has anyone had similar issues?

Xewdy444 commented 8 months ago

Could you please provide the full traceback of the error as this appears to be a bug with Playwright, similar to one that I've experienced: https://github.com/microsoft/playwright/issues/21780.

JackYouk commented 8 months ago

Yup same issue. Have you found a work-around? Turning headless mode off doesn't work for me either. Error Traceback when trying to access the iframe contextawait captcha_iframe.content_frame():

Traceback (most recent call last):
  File "/Users/jackjack/Documents/code/scrapers/vidburg/upload_puppet.py", line 77, in <module>
    asyncio.run(upload('bshhjsbd', 'kjbndskbsd', 'ksdbhsjhsbkh.mp4'))
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/Users/jackjack/Documents/code/scrapers/vidburg/upload_puppet.py", line 65, in snap_upload
    captcha_iframe = await captcha_iframe.content_frame()
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/playwright/async_api/_generated.py", line 1773, in content_frame
    return mapping.from_impl_nullable(await self._impl_obj.content_frame())
                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/playwright/_impl/_element_handle.py", line 68, in content_frame
    return from_nullable_channel(await self._channel.send("contentFrame"))
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/playwright/_impl/_connection.py", line 62, in send
    return await self._connection.wrap_api_call(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/playwright/_impl/_connection.py", line 492, in wrap_api_call
    return await cb()
           ^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/playwright/_impl/_connection.py", line 100, in inner_send
    result = next(iter(done)).result()
             ^^^^^^^^^^^^^^^^^^^^^^^^^
playwright._impl._errors.Error: Protocol error (Page.describeNode): error in channel "content::9/14/2": exception while running method "describeNode" in namespace "page": Permission denied to access property "docShell" on cross-origin object _describeNode@chrome://juggler/content/content/PageAgent.js:416:62
_onMessageInternal@chrome://juggler/content/SimpleChannel.js:237:37
_onMessage@chrome://juggler/content/SimpleChannel.js:194:12
bindToActor/actor.receiveMessage@chrome://juggler/content/SimpleChannel.js:39:44
Xewdy444 commented 8 months ago

Well, the only way around this error is to simply use either WebKit or Chromium instead of Firefox.

JackYouk commented 8 months ago

:( I get reCAPTCHA rate limit errors when I use WebKit... Here is my current implementation:'

captcha_iframe = await page.query_selector('iframe[title="reCAPTCHA"]')
   if captcha_iframe:
       print("reCAPTCHA detected. Solving reCAPTCHA...")
       captcha_iframe = await captcha_iframe.content_frame()
       with recaptchav2.SyncSolver(page, capsolver_api_key=CAPSOLVER_API_KEY) as solver:
           token = solver.solve_recaptcha(attempts=5, wait=True, image_challenge=True)
           print(token)
Xewdy444 commented 8 months ago

You could try using Botright, which uses Chromium or preferably Ungoogled Chromium with stealth capabilities.