From 73213fe8a1d07dea7b37ac6329c0a5a0c2f83d27 Mon Sep 17 00:00:00 2001 From: GaspardCulis Date: Sat, 17 Aug 2024 17:25:04 +0200 Subject: [PATCH] fix(account): Fixed `check_captcha` edge cases --- src/elevenlabs_unleashed/account.py | 32 +++++++++++++++++------------ 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/elevenlabs_unleashed/account.py b/src/elevenlabs_unleashed/account.py index aa87dc6..36b33ae 100644 --- a/src/elevenlabs_unleashed/account.py +++ b/src/elevenlabs_unleashed/account.py @@ -2,7 +2,7 @@ import names import requests import undetected_chromedriver as uc -from selenium.common.exceptions import JavascriptException +from selenium.common.exceptions import JavascriptException, NoSuchElementException from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.wait import WebDriverWait @@ -175,20 +175,26 @@ def submit(self): return super() def check_captcha(self): - captcha_iframe = self.driver.find_element(By.XPATH, "//iframe[@tabindex='0']") + try: + captcha_iframe = self.driver.find_element( + By.XPATH, "//iframe[@tabindex='0']" + ) - self.driver.switch_to.frame(captcha_iframe) - captcha_checkbox = self.wait.until( - lambda driver: driver.find_element(By.ID, "checkbox") - ) - # Wait for aria-checked to be true - t0 = monotonic() - while captcha_checkbox.get_attribute("aria-checked") == "false": - sleep(0.1) - if monotonic() - t0 > 20: - raise Exception("Captcha not checked in time") + self.driver.switch_to.frame(captcha_iframe) + captcha_checkbox = self.wait.until( + lambda driver: driver.find_element(By.ID, "checkbox") + ) + # Wait for aria-checked to be true + t0 = monotonic() + while captcha_checkbox.get_attribute("aria-checked") == "false": + sleep(0.1) + if monotonic() - t0 > 20: + raise Exception("Captcha not checked in time") + + self.driver.switch_to.default_content() + except NoSuchElementException: + pass - self.driver.switch_to.default_content() return self