-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler_webdriver.py
More file actions
1132 lines (939 loc) · 46.5 KB
/
euler_webdriver.py
File metadata and controls
1132 lines (939 loc) · 46.5 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
eulerdriver
automates solution to Project Euler
"""
import os
import time
import logging
import random
import base64
import threading
from typing import Optional, Dict, List, Tuple
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import (
TimeoutException,
NoSuchElementException,
WebDriverException,
ElementClickInterceptedException
)
from dotenv import load_dotenv
import openai
# load environment variables
load_dotenv()
class EulerWebdriver:
"""webdriver for automation"""
def __init__(self, headless: bool = False, action_delay: float = 1.0, max_retries: int = 3):
"""Initialize webdriver with settings"""
self.headless = headless
self.action_delay = action_delay
self.max_retries = max_retries
self.driver = None
self.wait = None
self.is_logged_in = False
# captcha settings
self.captcha_dir = os.path.join(os.getcwd(), "captcha")
os.makedirs(self.captcha_dir, exist_ok=True)
self.captcha_cleanup_timer = None
# logging setup
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('euler_webdriver.log', encoding='utf-8'),
logging.StreamHandler()
]
)
self.logger = logging.getLogger(__name__)
# urls
self.base_url = "https://projecteuler.net"
self.login_url = f"{self.base_url}/sign_in"
self.problems_url = f"{self.base_url}/archives"
self.progress_url = f"{self.base_url}/progress"
def _find_brave_executable(self) -> str:
"""Find Brave browser executable"""
possible_paths = [
r"C:\Program Files\BraveSoftware\Brave-Browser\Application\brave.exe",
r"C:\Program Files (x86)\BraveSoftware\Brave-Browser\Application\brave.exe",
r"C:\Users\{}\AppData\Local\BraveSoftware\Brave-Browser\Application\brave.exe".format(os.getenv('USERNAME')),
]
for path in possible_paths:
if os.path.exists(path):
self.logger.info(f"Found Brave at: {path}")
return path
raise FileNotFoundError("Brave browser not found. Please install Brave or check the path.")
def _download_chromedriver(self) -> str:
"""Download ChromeDriver if needed"""
import requests
import zipfile
import shutil
# latest ver
try:
response = requests.get("https://googlechromelabs.github.io/chrome-for-testing/LATEST_RELEASE_STABLE")
version = response.text.strip()
download_url = f"https://storage.googleapis.com/chrome-for-testing-public/{version}/win32/chromedriver-win32.zip"
except:
# old api fallback
try:
response = requests.get("https://chromedriver.storage.googleapis.com/LATEST_RELEASE")
version = response.text.strip()
download_url = f"https://chromedriver.storage.googleapis.com/{version}/chromedriver_win32.zip"
except:
# final fallback
version = "114.0.5735.90"
download_url = f"https://chromedriver.storage.googleapis.com/{version}/chromedriver_win32.zip"
self.logger.warning(f"Using fallback version: {version}")
# create temp directory
temp_dir = os.path.join(os.getcwd(), "chromedriver_temp")
os.makedirs(temp_dir, exist_ok=True)
chromedriver_path = os.path.join(temp_dir, "chromedriver.exe")
if not os.path.exists(chromedriver_path):
self.logger.info("Downloading ChromeDriver...")
try:
response = requests.get(download_url)
response.raise_for_status()
# save and extract
zip_path = os.path.join(temp_dir, "chromedriver.zip")
with open(zip_path, 'wb') as f:
f.write(response.content)
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(temp_dir)
# new api structure
if "chrome-for-testing" in download_url:
extracted_dir = os.path.join(temp_dir, "chromedriver-win32")
if os.path.exists(extracted_dir):
chromedriver_src = os.path.join(extracted_dir, "chromedriver.exe")
if os.path.exists(chromedriver_src):
shutil.move(chromedriver_src, chromedriver_path)
shutil.rmtree(extracted_dir)
os.remove(zip_path)
self.logger.info("ChromeDriver downloaded successfully")
except Exception as e:
self.logger.error(f"Failed to download ChromeDriver: {e}")
raise
return chromedriver_path
def _setup_driver(self) -> None:
"""Setup the Brave driver"""
try:
# find brave executable
brave_path = self._find_brave_executable()
# chrome options for brave
options = Options()
options.binary_location = brave_path
if self.headless:
options.add_argument('--headless')
# basic options
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-blink-features=AutomationControlled')
# custom user agent if provided
user_agent = os.getenv('USER_AGENT')
if user_agent:
options.add_argument(f'--user-agent={user_agent}')
# download chromedriver if needed
chromedriver_path = self._download_chromedriver()
# create service and driver
from selenium.webdriver.chrome.service import Service
service = Service(chromedriver_path)
self.driver = webdriver.Chrome(service=service, options=options)
# remove webdriver property for stealth
self.driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
# setup wait
self.wait = WebDriverWait(self.driver, 10)
self.logger.info("Brave webdriver setup completed")
except Exception as e:
self.logger.error(f"Failed to setup webdriver: {e}")
raise
def _human_delay(self, min_delay: float = None, max_delay: float = None) -> None:
"""Add human-like delay between actions"""
if min_delay is None:
min_delay = self.action_delay * 0.5
if max_delay is None:
max_delay = self.action_delay * 1.5
delay = random.uniform(min_delay, max_delay)
time.sleep(delay)
def _safe_click(self, element, retries: int = None) -> bool:
"""Safely click an element with retries"""
if retries is None:
retries = self.max_retries
for attempt in range(retries):
try:
# scroll element into view
self.driver.execute_script("arguments[0].scrollIntoView(true);", element)
self._human_delay(0.1, 0.3)
# try regular click first
element.click()
self._human_delay()
return True
except ElementClickInterceptedException:
try:
# try javascript click if regular click fails
self.driver.execute_script("arguments[0].click();", element)
self._human_delay()
return True
except Exception as e:
self.logger.warning(f"Click attempt {attempt + 1} failed: {e}")
if attempt < retries - 1:
self._human_delay(1, 2)
except Exception as e:
self.logger.warning(f"Click attempt {attempt + 1} failed: {e}")
if attempt < retries - 1:
self._human_delay(1, 2)
return False
def _safe_send_keys(self, element, text: str) -> bool:
"""Safely send keys to an element"""
try:
element.clear()
self._human_delay(0.1, 0.2)
# type with human-like delays
for char in text:
element.send_keys(char)
time.sleep(random.uniform(0.05, 0.15))
self._human_delay()
return True
except Exception as e:
self.logger.error(f"Failed to send keys: {e}")
return False
def _screenshot_captcha_element(self, captcha_element) -> Optional[str]:
"""
Take a screenshot of the captcha element to capture the EXACT currently displayed captcha
args:
captcha_element: The captcha image element
returns:
Optional[str]: Path to screenshot image or None if failed
"""
try:
# verify element is still valid and visible
if not captcha_element.is_displayed():
self.logger.error("Captcha element is not displayed")
return None
# ensure the element is visible and in view
self.driver.execute_script("arguments[0].scrollIntoView(true);", captcha_element)
self._human_delay(0.2, 0.3) # slightly longer delay to ensure rendering
# get element dimensions before screenshot
try:
size = captcha_element.size
location = captcha_element.location
self.logger.info(f"Captcha element dimensions: {size}, location: {location}")
# check if element has reasonable size
if size['width'] < 50 or size['height'] < 20:
self.logger.warning(f"Captcha element seems too small: {size}")
return None
except Exception as e:
self.logger.warning(f"Could not get element dimensions: {e}")
# take screenshot of the specific element
screenshot = captcha_element.screenshot_as_png
# verify we got a valid screenshot
if len(screenshot) < 1000: # increased threshold for valid captcha
self.logger.warning(f"Screenshot too small ({len(screenshot)} bytes), might be invalid")
return None
# save the screenshot
timestamp = int(time.time())
filename = f"captcha_screenshot_{timestamp}.png"
filepath = os.path.join(self.captcha_dir, filename)
with open(filepath, 'wb') as f:
f.write(screenshot)
self.logger.info(f"Captured captcha screenshot: {filepath} (size: {len(screenshot)} bytes)")
return filepath
except Exception as e:
self.logger.error(f"Failed to screenshot captcha element: {e}")
return None
def _delete_captcha_image(self, filepath: str) -> None:
"""
Delete captcha image immediately
args:
filepath: Path to the captcha image file
"""
try:
if os.path.exists(filepath):
os.remove(filepath)
self.logger.info(f"Deleted captcha image: {filepath}")
except Exception as e:
self.logger.error(f"Failed to delete captcha image {filepath}: {e}")
def _solve_captcha_with_openai(self, image_path: str) -> Optional[str]:
"""
Solve captcha using OpenAI API
args:
image_path: Path to the captcha image
returns:
Optional[str]: Captcha solution or None if failed
"""
try:
# check if OpenAI API key is available
api_key = os.getenv('OPENAI_API_KEY')
if not api_key:
self.logger.warning("OpenAI API key not found in environment variables")
return None
# initialize OpenAI client
client = openai.OpenAI(api_key=api_key)
# encode image to base64
with open(image_path, 'rb') as image_file:
base64_image = base64.b64encode(image_file.read()).decode('utf-8')
# call OpenAI API
response = client.chat.completions.create(
model="gpt-5-mini",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "I am having trouble reading this image. Return just the text displayed in this image with no spaces or trailing text. Expect 5-6 numbers."
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{base64_image}"
}
}
]
}
],
)
# extract solution
solution = response.choices[0].message.content.strip()
self.logger.info(f"OpenAI captcha solution: {solution}")
return solution
except Exception as e:
self.logger.error(f"Failed to solve captcha with OpenAI: {e}")
return None
def start(self) -> None:
"""Start the webdriver"""
self._setup_driver()
self.logger.info("Euler Webdriver started")
def stop(self) -> None:
"""Stop the webdriver"""
if self.driver:
self.driver.quit()
self.logger.info("Euler Webdriver stopped")
def __enter__(self):
"""Context manager entry"""
self.start()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"""Context manager exit"""
self.stop()
def login(self) -> bool:
"""
try login to project euler with persistent browser session
returns:
bool: True if login successful, False otherwise
"""
try:
# check if already logged in (persistent session)
if self.check_login_status():
self.logger.info("Already logged in (persistent session)")
return True
username = os.getenv('EULER_USERNAME')
password = os.getenv('EULER_PASSWORD')
if not username or not password:
self.logger.error("Username or password not found in environment variables")
return False
self.logger.info("Attempting to login to Project Euler...")
# navigate to login page
self.driver.get(self.login_url)
self._human_delay(1, 2)
# find and fill username field
username_field = self.wait.until(
EC.presence_of_element_located((By.NAME, "username"))
)
if not self._safe_send_keys(username_field, username):
self.logger.error("Failed to enter username")
return False
# find and fill password field
password_field = self.driver.find_element(By.NAME, "password")
if not self._safe_send_keys(password_field, password):
self.logger.error("Failed to enter password")
return False
# check "remember me" checkbox if present
try:
remember_me_selectors = [
"//input[@type='checkbox' and contains(@name, 'remember')]",
"//input[@type='checkbox' and contains(@id, 'remember')]",
"//input[@type='checkbox' and contains(@class, 'remember')]"
]
for selector in remember_me_selectors:
try:
remember_checkbox = self.driver.find_element(By.XPATH, selector)
if not remember_checkbox.is_selected():
self._safe_click(remember_checkbox)
self.logger.info("Checked 'Remember Me' checkbox")
break
except NoSuchElementException:
continue
except Exception as e:
self.logger.debug(f"Could not find or check 'Remember Me' checkbox: {e}")
# handle captcha if present (but don't check for failure yet)
captcha_handled = self._handle_captcha_if_present(max_retries=3)
if not captcha_handled:
self.logger.error("Failed to handle captcha after retries")
return False
# find and click login button with multiple selectors
login_button = None
login_selectors = [
"//input[@name='sign_in']",
"//input[@type='submit' and @value='Sign In']",
"//input[@type='submit']",
"//input[@value='Sign In']",
"//input[@value='Login']",
"//button[@type='submit']",
"//button[contains(text(), 'Sign In')]",
"//button[contains(text(), 'Login')]"
]
for selector in login_selectors:
try:
login_button = self.driver.find_element(By.XPATH, selector)
self.logger.info(f"Found login button with selector: {selector}")
break
except NoSuchElementException:
continue
if not login_button:
self.logger.error("Could not find login button")
return False
if not self._safe_click(login_button):
self.logger.error("Failed to click login button")
return False
# wait for form submission and redirect
self._human_delay(1.5, 2.5)
# check if we're redirected away from login page
current_url = self.driver.current_url
if "sign_in" not in current_url:
self.is_logged_in = True
self.logger.info("Login successful!")
return True
else:
# still on login page - check for specific error messages
error_found = False
# check for captcha failure messages
captcha_error_messages = [
"The confirmation code you entered was not valid",
"You did not enter the confirmation code",
"Invalid confirmation code",
"Captcha verification failed"
]
page_text = self.driver.page_source.lower()
for error_msg in captcha_error_messages:
if error_msg.lower() in page_text:
self.logger.error(f"Captcha failed: {error_msg}")
error_found = True
break
# check for general error messages
if not error_found:
try:
error_elements = self.driver.find_elements(By.CLASS_NAME, "error")
for error_element in error_elements:
error_text = error_element.text.strip()
if error_text:
self.logger.error(f"Login failed: {error_text}")
error_found = True
break
except NoSuchElementException:
pass
if not error_found:
self.logger.error("Login failed: Still on login page but no specific error found")
return False
except TimeoutException:
self.logger.error("Login timeout - page elements not found")
return False
except Exception as e:
self.logger.error(f"Login failed with exception: {e}")
return False
def check_login_status(self) -> bool:
"""
Check if currently logged in to Project Euler
Returns:
bool: True if logged in, False otherwise
"""
try:
# navigate to main page
self.driver.get(self.base_url)
self._human_delay(1, 2)
# look for logout link
try:
# check for logout link (indicates logged in)
logout_element = self.driver.find_element(By.XPATH, "//a[contains(@href, 'sign_out')]")
self.is_logged_in = True
self.logger.info("Already logged in")
return True
except NoSuchElementException:
# check for sign in link (indicates not logged in)
try:
signin_element = self.driver.find_element(By.XPATH, "//a[contains(@href, 'sign_in')]")
self.is_logged_in = False
self.logger.info("Not logged in")
return False
except NoSuchElementException:
# if neither found, assume logged in
self.is_logged_in = True
return True
except Exception as e:
self.logger.error(f"Error checking login status: {e}")
return False
def navigate_to_problem(self, problem_number: int) -> bool:
"""
go directly to a specific problem page
args:
problem_number: The problem number to navigate to
returns:
bool: True if navigation successful, False otherwise
"""
try:
self.logger.info(f"Navigating to problem {problem_number}...")
# navigate directly to the problem URL
problem_url = f"{self.base_url}/problem={problem_number}"
self.driver.get(problem_url)
self._human_delay(0.5, 1.0)
# verify we're on the correct problem page
current_url = self.driver.current_url
if str(problem_number) in current_url or f"problem={problem_number}" in current_url:
self.logger.info(f"Successfully navigated to problem {problem_number}")
return True
else:
self.logger.error(f"Navigation verification failed for problem {problem_number}")
return False
except Exception as e:
self.logger.error(f"Failed to navigate to problem {problem_number}: {e}")
return False
def get_next_unsolved_problem(self) -> Optional[int]:
"""
get the next unsolved problem from the progress page
returns:
Optional[int]: Next unsolved problem number, or None if none found
"""
try:
self.logger.info("Finding next unsolved problem from progress page...")
# navigate to progress page
self.driver.get(self.progress_url)
self._human_delay(1, 1.5)
try:
# use the most efficient selector - direct td elements with problem links
problem_elements = self.driver.find_elements(By.XPATH, "//td[@class='tooltip problem_unsolved']//a[contains(@href, 'problem=')]")
if not problem_elements:
# fallback: look for any unsolved problem links
problem_elements = self.driver.find_elements(By.XPATH, "//a[contains(@href, 'problem=')]")
self.logger.info(f"Found {len(problem_elements)} problem elements, checking for first unsolved...")
for element in problem_elements:
try:
# extract problem number
href = element.get_attribute('href')
if href and 'problem=' in href:
problem_num = int(href.split('problem=')[1].split('&')[0])
# check if this problem is unsolved (fast check)
parent_td = element.find_element(By.XPATH, "./..")
td_class = parent_td.get_attribute('class') or ''
# if it has 'problem_unsolved' class, it's unsolved
if 'problem_unsolved' in td_class:
self.logger.info(f"Found next unsolved problem: {problem_num}")
return problem_num
# if it has 'problem_solved' class, skip it
elif 'problem_solved' in td_class:
continue
# if no class info, check style for orange background
else:
td_style = parent_td.get_attribute('style') or ''
if 'rgb(255, 186, 0)' not in td_style and 'orange' not in td_style.lower():
self.logger.info(f"Found next unsolved problem: {problem_num} (no solved styling)")
return problem_num
except Exception as e:
self.logger.debug(f"Error processing problem element: {e}")
continue
self.logger.info("No unsolved problems found")
return None
except Exception as e:
self.logger.error(f"Error parsing progress page: {e}")
return None
except Exception as e:
self.logger.error(f"Error getting next unsolved problem: {e}")
return None
def submit_answer(self, answer: str) -> Tuple[bool, str]:
"""
submit an answer for the current problem
args:
answer: The answer to submit
returns:
Tuple[bool, str]: (success, message) - success indicates if submission worked, message contains result
"""
try:
self.logger.info(f"Submitting answer: {answer}")
# check for captcha before submitting
captcha_handled = self._handle_captcha_if_present(max_retries=3)
if not captcha_handled:
self.logger.error("Failed to handle captcha before submission")
return False, "Captcha handling failed"
# look for answer input field
answer_selectors = [
"//input[@name='answer']",
"//input[@id='answer']",
"//input[@type='text']",
"//textarea[@name='answer']"
]
answer_field = None
for selector in answer_selectors:
try:
# use a shorter timeout for answer field lookup to reduce delay
quick_wait = WebDriverWait(self.driver, 2)
answer_field = quick_wait.until(
EC.presence_of_element_located((By.XPATH, selector))
)
break
except TimeoutException:
continue
if not answer_field:
self.logger.error("Could not find answer input field")
return False, "Answer field not found"
# enter the answer
if not self._safe_send_keys(answer_field, answer):
self.logger.error("Failed to enter answer")
return False, "Failed to enter answer"
# look for submit button
submit_selectors = [
"//input[@type='submit']",
"//button[@type='submit']",
"//input[@value='Submit']",
"//button[contains(text(), 'Submit')]"
]
submit_button = None
for selector in submit_selectors:
try:
submit_button = self.driver.find_element(By.XPATH, selector)
break
except NoSuchElementException:
continue
if not submit_button:
self.logger.error("Could not find submit button")
return False, "Submit button not found"
# click submit button
if not self._safe_click(submit_button):
self.logger.error("Failed to click submit button")
return False, "Failed to submit answer"
# wait for response
self._human_delay(0.5, 1.0)
# check for result message
result_message = self._check_submission_result()
return True, result_message
except Exception as e:
self.logger.error(f"Failed to submit answer: {e}")
return False, f"Submission error: {e}"
def _check_submission_result(self) -> str:
"""
check the result of answer submission
returns:
str: Result message
"""
try:
# check for rate limiting first
is_limited, wait_time = self.is_rate_limited()
if is_limited:
if wait_time:
self.logger.warning(f"Rate limited after submission, need to wait {wait_time} seconds...")
else:
self.logger.warning("Rate limited after submission, refreshing page...")
if self.wait_for_rate_limit():
return "Rate limit cleared, ready for next submission"
else:
return "Rate limited and unable to clear"
# quick check for obvious success/failure indicators
page_text = self.driver.page_source.lower()
if 'correct' in page_text and 'congratulations' in page_text:
return "Correct! Congratulations!"
elif 'incorrect' in page_text:
return "Incorrect answer"
elif 'already solved' in page_text:
return "Problem already solved"
else:
return "Submission completed"
except Exception as e:
self.logger.error(f"Error checking submission result: {e}")
return f"Error checking result: {e}"
def _parse_wait_time_from_message(self, message: str) -> Optional[int]:
"""
Parse wait time in seconds from rate limit error message
args:
message: The error message containing wait time information
returns:
Optional[int]: Wait time in seconds, or None if not found
"""
import re
try:
# convert to lowercase for case-insensitive matching
message_lower = message.lower()
# pattern to match "X minute(s), Y second(s)" format
minute_second_pattern = r'(\d+)\s+minute(?:s)?,?\s+(\d+)\s+second(?:s)?'
match = re.search(minute_second_pattern, message_lower)
if match:
minutes = int(match.group(1))
seconds = int(match.group(2))
total_seconds = minutes * 60 + seconds
self.logger.info(f"Parsed wait time: {minutes} minutes, {seconds} seconds = {total_seconds} total seconds")
return total_seconds
# pattern to match just seconds "X second(s)"
second_only_pattern = r'(\d+)\s+second(?:s)?'
match = re.search(second_only_pattern, message_lower)
if match:
seconds = int(match.group(1))
self.logger.info(f"Parsed wait time: {seconds} seconds")
return seconds
# pattern to match just minutes "X minute(s)" (convert to seconds)
minute_only_pattern = r'(\d+)\s+minute(?:s)?'
match = re.search(minute_only_pattern, message_lower)
if match:
minutes = int(match.group(1))
total_seconds = minutes * 60
self.logger.info(f"Parsed wait time: {minutes} minutes = {total_seconds} seconds")
return total_seconds
return None
except Exception as e:
self.logger.error(f"Error parsing wait time from message: {e}")
return None
def is_rate_limited(self) -> Tuple[bool, Optional[int]]:
"""
check if currently rate limited and extract wait time if available
returns:
Tuple[bool, Optional[int]]: (is_rate_limited, wait_time_seconds)
"""
try:
page_text = self.driver.page_source.lower()
rate_limit_indicators = [
'rate limit',
'too many',
'please wait',
'try again later',
'slow down',
'you must wait',
'before submitting any more answers'
]
for indicator in rate_limit_indicators:
if indicator in page_text:
self.logger.warning("Rate limit detected")
# try to extract wait time from the page content
wait_time = self._parse_wait_time_from_message(page_text)
if wait_time:
self.logger.info(f"Extracted wait time: {wait_time} seconds")
return True, wait_time
else:
self.logger.warning("Rate limit detected but could not parse wait time")
return True, None
return False, None
except Exception as e:
self.logger.error(f"Error checking rate limit: {e}")
return False, None
def wait_for_rate_limit(self, max_wait_time: int = 300) -> bool:
"""
wait for rate limit to clear using precise timing based on extracted wait time
args:
max_wait_time: Maximum time to wait in seconds (fallback if no specific time found)
returns:
bool: True if rate limit cleared, False if timeout
"""
try:
# check current rate limit status and extract wait time
is_limited, wait_time = self.is_rate_limited()
if not is_limited:
self.logger.info("No rate limit detected")
return True
# determine how long to wait
if wait_time is not None:
# use the specific wait time from the error message
actual_wait_time = min(wait_time, max_wait_time) # don't exceed max_wait_time
self.logger.info(f"Rate limit detected with specific wait time: {wait_time} seconds")
self.logger.info(f"Will wait for {actual_wait_time} seconds before refreshing...")
else:
# fallback to a reasonable default wait time
actual_wait_time = min(60, max_wait_time) # Default to 1 minute or max_wait_time
self.logger.warning(f"Rate limit detected but no specific wait time found")
self.logger.info(f"Will wait for {actual_wait_time} seconds before refreshing...")
# wait for the specified time with progress updates
self._wait_with_progress(actual_wait_time)
# refresh the page after waiting
self.logger.info("Wait time completed, refreshing page...")
self.driver.refresh()
self._human_delay(2, 3)
# check if rate limit is cleared
is_limited_after, _ = self.is_rate_limited()
if not is_limited_after:
self.logger.info("Rate limit cleared after timed wait and refresh!")
return True
else:
self.logger.warning("Still rate limited after timed wait, trying one more refresh...")
# one more refresh attempt
time.sleep(5)
self.driver.refresh()
self._human_delay(2, 3)
is_limited_final, _ = self.is_rate_limited()
if not is_limited_final:
self.logger.info("Rate limit cleared after second refresh!")
return True
else:
self.logger.error("Still rate limited after multiple refreshes and timed wait")
return False
except Exception as e:
self.logger.error(f"Error handling rate limit: {e}")
return False
def _wait_with_progress(self, wait_seconds: int) -> None:
"""
Wait for specified seconds with progress updates
args:
wait_seconds: Number of seconds to wait
"""
try:
# show progress updates every 10 seconds for waits longer than 30 seconds
if wait_seconds > 30:
update_interval = 10
remaining = wait_seconds
while remaining > 0:
if remaining <= update_interval:
# final wait
self.logger.info(f"Final wait: {remaining} seconds remaining...")
time.sleep(remaining)
break
else:
# wait for update interval
self.logger.info(f"Rate limit wait: {remaining} seconds remaining...")
time.sleep(update_interval)
remaining -= update_interval
else:
# for shorter waits, just wait without progress updates
self.logger.info(f"Waiting {wait_seconds} seconds for rate limit to clear...")
time.sleep(wait_seconds)
except Exception as e:
self.logger.error(f"Error during wait: {e}")
# fallback to simple sleep
time.sleep(wait_seconds)
def _solve_captcha(self) -> Optional[str]:
"""
solve captcha using automated OpenAI API with fallback to manual input
returns:
Optional[str]: Captcha solution or None if failed
"""
try:
captcha_img = None
try:
captcha_img = self.driver.find_element(By.ID, "captcha_image")
self.logger.info("Found captcha image using id='captcha_image'")
except NoSuchElementException:
self.logger.info("No captcha image found with id='captcha_image', trying fallback selectors...")
# fallback to other selectors
captcha_selectors = [
"//img[contains(@id, 'captcha')]",
"//img[contains(@src, 'captcha')]",
"//img[contains(@alt, 'captcha')]",
"//img[contains(@class, 'captcha')]"
]
for selector in captcha_selectors:
try:
captcha_img = self.driver.find_element(By.XPATH, selector)
self.logger.info(f"Found captcha image using selector: {selector}")
break
except NoSuchElementException: