-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathclick_interstitial.py
More file actions
100 lines (83 loc) · 3.75 KB
/
Copy pathclick_interstitial.py
File metadata and controls
100 lines (83 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import asyncio
import logging
from playwright.async_api import async_playwright
from playwright_captcha import CaptchaType, ClickSolver, FrameworkType
logging.basicConfig(
level='INFO',
format='[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s',
datefmt='%H:%M:%S'
)
async def solve_interstitial() -> None:
# Note: click-based solvers may not reliably solve captchas in Playwright, as it isn't stealthy enough
# For better results, use a stealth browser like camoufox/patchright (https://github.com/daijro/camoufox) - example below
# usage with patchright:
# from patchright.async_api import async_playwright
# async with async_playwright() as playwright:
# browser = await playwright.chromium.launch(
# channel="chrome",
# headless=True,
# )
# context = await browser.new_context()
# page = await context.new_page()
#
# framework = FrameworkType.PATCHRIGHT
#
# ...
# usage with camoufox:
# ! At the moment, there is a problem with `add_init_script` method in camoufox
# ! which is needed for the captcha solving, so we use my temporary workaround:
# ! https://github.com/techinz/camoufox-add_init_script
# ! It is already pre-installed and automatically used in the playwright-captcha package.
# ! You just need to add the addon when creating Camoufox instance
# from camoufox import AsyncCamoufox
# from playwright_captcha.utils.camoufox_add_init_script.add_init_script import get_addon_path
# import os
#
# ADDON_PATH = get_addon_path() # path to the addon script
#
# async with AsyncCamoufox(
# headless=True,
# geoip=True,
# humanize=True,
#
# i_know_what_im_doing=True,
# config={'forceScopeAccess': True}, # add this when creating Camoufox instance
# disable_coop=True, # add this when creating Camoufox instance
#
# main_world_eval=True, # 1. (only for camoufox) add this to use `add_init_script` temporary workaround
# addons=[os.path.abspath(ADDON_PATH)]
# # 2. (only for camoufox) add this to use `add_init_script` temporary workaround
# ) as browser:
# context = await browser.new_context()
# page = await context.new_page()
#
# framework = FrameworkType.CAMOUFOX
#
# ...
# usage with playwright (used in this example):
async with async_playwright() as playwright:
browser = await playwright.chromium.launch(headless=False)
page = await browser.new_page()
framework = FrameworkType.PLAYWRIGHT
# create a solver instance before navigating to the page
async with ClickSolver(framework=framework, page=page) as solver:
await page.goto('https://2captcha.com/demo/cloudflare-turnstile-challenge')
# wait for the page to load completely (could be replaced with a more robust check in a real scenario)
await asyncio.sleep(5)
# solve the captcha
await solver.solve_captcha(
captcha_container=page,
captcha_type=CaptchaType.CLOUDFLARE_INTERSTITIAL,
expected_content_selector="#root"
)
await asyncio.sleep(5) # wait for the result to be processed
# additionally check if the captcha was solved successfully
try:
await page.locator('//p[text()="Captcha is passed successfully!"]').wait_for(timeout=10000)
logging.info('Captcha is passed successfully!')
except Exception as e:
logging.error(f'Captcha solving failed: {e}')
return
logging.info('Finished')
if __name__ == '__main__':
asyncio.run(solve_interstitial())