@@ -12,17 +12,10 @@ def setup_driver():
12
12
print ("Setting up Chrome driver..." )
13
13
chrome_options = Options ()
14
14
15
- # Required arguments for running in GitHub Actions
16
- chrome_options .add_argument ('--headless=new' ) # Updated headless argument
15
+ chrome_options .add_argument ('--headless=new' )
17
16
chrome_options .add_argument ('--no-sandbox' )
18
17
chrome_options .add_argument ('--disable-dev-shm-usage' )
19
- chrome_options .add_argument ('--disable-gpu' )
20
18
chrome_options .add_argument ('--window-size=1920,1080' )
21
- chrome_options .add_argument ('--disable-extensions' )
22
-
23
- # Additional debugging options
24
- chrome_options .add_argument ('--verbose' )
25
- chrome_options .add_argument ('--log-level=0' )
26
19
27
20
print ("Chrome options configured, initializing driver..." )
28
21
@@ -36,64 +29,57 @@ def setup_driver():
36
29
raise
37
30
38
31
39
- def take_element_screenshot (driver , url , selector , output_name ):
32
+ def take_screenshot (driver , url , cookie_value , selector , output_name ):
40
33
print (f"Attempting to take screenshot of { selector } at { url } " )
41
34
os .makedirs ('screenshots' , exist_ok = True )
42
35
43
36
try :
44
- print ( "Navigating to page..." )
45
- driver .get (url )
37
+ # First navigate to the domain to set cookie
38
+ driver .get ("https://adventofcode.com" )
46
39
47
- # Add a small delay to ensure page loads
48
- time .sleep (5 )
49
- print ("Page loaded, waiting for element..." )
40
+ # Set the session cookie
41
+ driver .add_cookie ({
42
+ 'name' : 'session' ,
43
+ 'value' : cookie_value ,
44
+ 'domain' : '.adventofcode.com'
45
+ })
46
+
47
+ print ("Cookie set, navigating to target page..." )
48
+ driver .get (url )
49
+ time .sleep (3 ) # Allow page to load
50
50
51
- # Wait for element to be present and visible
52
- element = WebDriverWait (driver , 20 ).until (
51
+ print ( "Waiting for element..." )
52
+ element = WebDriverWait (driver , 10 ).until (
53
53
EC .presence_of_element_located ((By .CSS_SELECTOR , selector ))
54
54
)
55
55
56
- print ("Element found, preparing for screenshot..." )
57
-
58
- # Ensure element is visible in viewport
59
- driver .execute_script ("arguments[0].scrollIntoView();" , element )
60
- time .sleep (2 ) # Give time for any animations to complete
61
-
62
- # Take full page screenshot first for debugging
63
- driver .save_screenshot ('screenshots/full_page.png' )
64
- print ("Full page screenshot saved for debugging" )
65
-
66
- # Take screenshot of specific element
56
+ print ("Element found, taking screenshot..." )
67
57
element .screenshot (f'screenshots/{ output_name } .png' )
68
- print (f"Element screenshot saved as screenshots/{ output_name } .png" )
58
+ print (f"Screenshot saved as screenshots/{ output_name } .png" )
69
59
70
60
except Exception as e :
71
61
print (f"Error during screenshot process: { str (e )} " )
72
62
print (f"Current URL: { driver .current_url } " )
73
- print (f"Page source length: { len (driver .page_source )} " )
74
63
raise
75
64
76
65
77
66
def main ():
78
67
print ("Starting screenshot process..." )
79
68
driver = None
80
- try :
81
- driver = setup_driver ()
82
69
83
- # Test with a simple, reliable page first
84
- take_element_screenshot (
85
- driver = driver ,
86
- url = 'https://example.com' , # Using example.com as a test
87
- selector = 'h1' , # Example.com has a simple h1 element
88
- output_name = 'test-screenshot'
89
- )
70
+ # Get cookie from environment variable
71
+ cookie = os .getenv ('COOKIE' )
72
+ if not cookie :
73
+ raise ValueError ("COOKIE environment variable not set" )
90
74
91
- # If the above works, try your actual target
92
- take_element_screenshot (
75
+ try :
76
+ driver = setup_driver ()
77
+ take_screenshot (
93
78
driver = driver ,
94
- url = 'https://github.com' ,
95
- selector = '.header' ,
96
- output_name = 'github-header'
79
+ url = 'https://adventofcode.com/2024' ,
80
+ cookie_value = cookie ,
81
+ selector = 'body > main > pre' ,
82
+ output_name = 'aoc-content'
97
83
)
98
84
99
85
except Exception as e :
0 commit comments