Skip to content

Commit

Permalink
Merge pull request #4 from alexreg/issue-3
Browse files Browse the repository at this point in the history
Check for frame getting detached
  • Loading branch information
Xewdy444 authored Jan 28, 2023
2 parents 557e48f + 0a774e5 commit 393d7d4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
23 changes: 13 additions & 10 deletions playwright_recaptcha/recaptchav2/async_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class AsyncSolver:
def __init__(self, page: Page, retries: int = 3) -> None:
self._page = page
self._retries = retries
self.token = None
self.token: Optional[str] = None

def __repr__(self) -> str:
return f"AsyncSolver(page={self._page!r}, retries={self._retries!r})"
Expand Down Expand Up @@ -206,16 +206,16 @@ async def _submit_audio_text(
await textbox.fill(text)
await verify_button.click()

while True:
while not recaptcha_frame.is_detached():
solve_failure = recaptcha_frame.get_by_text(
"Multiple correct solutions required - please solve more."
)

rate_limit = recaptcha_frame.get_by_text("Try again later")

if (
await solve_failure.is_visible()
or await recaptcha_checkbox.is_checked()
await recaptcha_checkbox.is_checked()
or await solve_failure.is_visible()
):
break

Expand Down Expand Up @@ -259,6 +259,9 @@ async def solve_recaptcha(self, retries: Optional[int] = None) -> str:
await recaptcha_checkbox.click()

if await recaptcha_checkbox.is_checked():
if self.token is None:
raise RecaptchaSolveError

return self.token

while retries > 0:
Expand All @@ -268,8 +271,11 @@ async def solve_recaptcha(self, retries: Optional[int] = None) -> str:
await self._random_delay()
await self._submit_audio_text(recaptcha_frame, recaptcha_checkbox, text)

if await recaptcha_checkbox.is_checked():
break
if recaptcha_frame.is_detached() or await recaptcha_checkbox.is_checked():
if self.token is None:
raise RecaptchaSolveError

return self.token

new_challenge_button = recaptcha_frame.get_by_role(
"button", name="Get a new challenge"
Expand All @@ -278,7 +284,4 @@ async def solve_recaptcha(self, retries: Optional[int] = None) -> str:
await new_challenge_button.click()
retries -= 1

if not await recaptcha_checkbox.is_checked():
raise RecaptchaSolveError

return self.token
raise RecaptchaSolveError
23 changes: 13 additions & 10 deletions playwright_recaptcha/recaptchav2/sync_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class SyncSolver:
def __init__(self, page: Page, retries: int = 3) -> None:
self._page = page
self._retries = retries
self.token = None
self.token: Optional[str] = None

def __repr__(self) -> str:
return f"SyncSolver(page={self._page!r}, retries={self._retries!r})"
Expand Down Expand Up @@ -177,12 +177,12 @@ def _submit_audio_text(
recaptcha_frame.get_by_role("textbox", name="Enter what you hear").fill(text)
recaptcha_frame.get_by_role("button", name="Verify").click()

while True:
while not recaptcha_frame.is_detached():
if (
recaptcha_frame.get_by_text(
recaptcha_checkbox.is_checked()
or recaptcha_frame.get_by_text(
"Multiple correct solutions required - please solve more."
).is_visible()
or recaptcha_checkbox.is_checked()
):
break

Expand Down Expand Up @@ -226,6 +226,9 @@ def solve_recaptcha(self, retries: Optional[int] = None) -> str:
recaptcha_checkbox.click()

if recaptcha_checkbox.is_checked():
if self.token is None:
raise RecaptchaSolveError

return self.token

while retries > 0:
Expand All @@ -235,13 +238,13 @@ def solve_recaptcha(self, retries: Optional[int] = None) -> str:
self._random_delay()
self._submit_audio_text(recaptcha_frame, recaptcha_checkbox, text)

if recaptcha_checkbox.is_checked():
break
if recaptcha_frame.is_detached() or recaptcha_checkbox.is_checked():
if self.token is None:
raise RecaptchaSolveError

return self.token

recaptcha_frame.get_by_role("button", name="Get a new challenge").click()
retries -= 1

if not recaptcha_checkbox.is_checked():
raise RecaptchaSolveError

return self.token
raise RecaptchaSolveError
2 changes: 1 addition & 1 deletion playwright_recaptcha/recaptchav3/async_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class AsyncSolver:
def __init__(self, page: Page, timeout: int = 30) -> None:
self._page = page
self._timeout = timeout
self.token = None
self.token: Optional[str] = None

def __repr__(self) -> str:
return f"AsyncSolver(page={self._page!r}, timeout={self._timeout!r})"
Expand Down
2 changes: 1 addition & 1 deletion playwright_recaptcha/recaptchav3/sync_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SyncSolver:
def __init__(self, page: Page, timeout: int = 30) -> None:
self._page = page
self._timeout = timeout
self.token = None
self.token: Optional[str] = None

def __repr__(self) -> str:
return f"SyncSolver(page={self._page!r}, timeout={self._timeout!r})"
Expand Down

0 comments on commit 393d7d4

Please sign in to comment.