@@ -17,49 +17,107 @@ def setup_driver():
17
17
chrome_options .add_argument ('--disable-dev-shm-usage' )
18
18
chrome_options .add_argument ('--window-size=1920,1080' )
19
19
20
+ # Add user agent to avoid detection
21
+ chrome_options .add_argument (
22
+ '--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36' )
23
+
20
24
print ("Chrome options configured, initializing driver..." )
21
25
22
26
try :
23
27
service = Service ()
24
28
driver = webdriver .Chrome (service = service , options = chrome_options )
29
+ driver .set_page_load_timeout (30 )
25
30
print ("Chrome driver initialized successfully" )
26
31
return driver
27
32
except Exception as e :
28
33
print (f"Failed to initialize Chrome driver: { str (e )} " )
29
34
raise
30
35
31
36
37
+ def verify_cookie (cookie_value ):
38
+ if not cookie_value :
39
+ raise ValueError ("Cookie value is empty" )
40
+ print (f"Cookie length: { len (cookie_value )} characters" )
41
+ return cookie_value
42
+
43
+
44
+ def check_page_state (driver ):
45
+ """Debug page state"""
46
+ print ("\n Page debugging information:" )
47
+ print (f"Current URL: { driver .current_url } " )
48
+ print (f"Page title: { driver .title } " )
49
+ print ("\n First 500 characters of page source:" )
50
+ print (driver .page_source [:500 ])
51
+
52
+ # Try to find any elements to verify page is loading
53
+ all_elements = driver .find_elements (By .TAG_NAME , "*" )
54
+ print (f"\n Total elements found on page: { len (all_elements )} " )
55
+
56
+ # Try to find main element
57
+ main_elements = driver .find_elements (By .TAG_NAME , "main" )
58
+ print (f"Number of <main> elements: { len (main_elements )} " )
59
+
60
+ # Try to find pre elements
61
+ pre_elements = driver .find_elements (By .TAG_NAME , "pre" )
62
+ print (f"Number of <pre> elements: { len (pre_elements )} " )
63
+
64
+
32
65
def take_screenshot (driver , url , cookie_value , selector , output_name ):
33
66
print (f"Attempting to take screenshot of { selector } at { url } " )
34
67
os .makedirs ('screenshots' , exist_ok = True )
35
68
36
69
try :
37
- # First navigate to the domain to set cookie
70
+ # Verify cookie before using
71
+ cookie_value = verify_cookie (cookie_value )
72
+
73
+ # First navigate to the domain root
74
+ print ("Navigating to domain root..." )
38
75
driver .get ("https://adventofcode.com" )
39
76
40
77
# Set the session cookie
78
+ print ("Setting cookie..." )
41
79
driver .add_cookie ({
42
80
'name' : 'session' ,
43
81
'value' : cookie_value ,
44
82
'domain' : '.adventofcode.com'
45
83
})
46
84
47
- print ("Cookie set, navigating to target page..." )
85
+ # Get cookies for debugging
86
+ cookies = driver .get_cookies ()
87
+ print (f"Current cookies: { [cookie ['name' ] for cookie in cookies ]} " )
88
+
89
+ print (f"Navigating to target URL: { url } " )
48
90
driver .get (url )
49
- time .sleep (3 ) # Allow page to load
91
+
92
+ # Take full page screenshot for debugging
93
+ driver .save_screenshot (f'screenshots/{ output_name } _full.png' )
94
+ print ("Saved full page screenshot for debugging" )
95
+
96
+ # Check page state
97
+ check_page_state (driver )
50
98
51
99
print ("Waiting for element..." )
52
- element = WebDriverWait (driver , 10 ).until (
53
- EC .presence_of_element_located ((By .CSS_SELECTOR , selector ))
54
- )
100
+ # Try different methods to find the element
101
+ try :
102
+ # First try with explicit wait
103
+ element = WebDriverWait (driver , 15 ).until (
104
+ EC .presence_of_element_located ((By .CSS_SELECTOR , selector ))
105
+ )
106
+ except Exception as e :
107
+ print (f"Failed with explicit wait: { str (e )} " )
108
+ print ("Trying direct find_element..." )
109
+ # Try direct find_element
110
+ element = driver .find_element (By .CSS_SELECTOR , selector )
55
111
56
112
print ("Element found, taking screenshot..." )
113
+ driver .execute_script ("arguments[0].scrollIntoView();" , element )
114
+ time .sleep (2 )
57
115
element .screenshot (f'screenshots/{ output_name } .png' )
58
116
print (f"Screenshot saved as screenshots/{ output_name } .png" )
59
117
60
118
except Exception as e :
61
119
print (f"Error during screenshot process: { str (e )} " )
62
- print ( f"Current URL: { driver . current_url } " )
120
+ check_page_state ( driver ) # Get debug info even on failure
63
121
raise
64
122
65
123
0 commit comments