Skip to content

Commit 96fd7b8

Browse files
committed
testing actions
1 parent c7f1796 commit 96fd7b8

File tree

1 file changed

+12
-23
lines changed

1 file changed

+12
-23
lines changed

.github/scripts/take_screenshot.py

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
import time
2+
13
from selenium import webdriver
24
from selenium.webdriver.chrome.options import Options
35
from selenium.webdriver.common.by import By
46
from selenium.webdriver.support.ui import WebDriverWait
57
from selenium.webdriver.support import expected_conditions as EC
68
from PIL import Image
79
import os
8-
import time
910

1011

11-
# Initialize driver with options and cookie
1212
def setup_driver(cookie_value):
1313
options = Options()
1414
options.add_argument('--headless=new')
@@ -19,42 +19,31 @@ def setup_driver(cookie_value):
1919
return driver
2020

2121

22-
# Crop the screenshot using Pillow
2322
def crop_image(input_path, output_path, crop_box=(0, 0, 640, 621)):
2423
with Image.open(input_path) as img:
2524
cropped = img.crop(crop_box)
2625
cropped.save(output_path)
2726

2827

29-
# Take screenshot of an element and trim it
3028
def take_screenshot(driver, url, selector, output_name):
3129
driver.get(url)
32-
time.sleep(3) # Wait for the page to load completely
33-
try:
34-
# Wait for the element to be present and visible
35-
element = WebDriverWait(driver, 20).until(
36-
EC.presence_of_element_located((By.CSS_SELECTOR, selector))
37-
)
38-
temp_output = output_name # Directly save the temp screenshot as the final output
39-
element.screenshot(temp_output)
40-
crop_image(temp_output, output_name)
41-
except Exception as e:
42-
print(f"Error: Timeout or issue locating element with selector '{selector}'")
43-
print(f'Current URL: {driver.current_url}')
44-
print('Page source:')
45-
print(driver.page_source[:1000]) # Print first 1000 characters of source for debugging
46-
raise
30+
element = WebDriverWait(driver, 10).until(
31+
EC.presence_of_element_located((By.CSS_SELECTOR, selector))
32+
)
33+
temp_output = output_name # Directly save the temp screenshot as the final output
34+
element.screenshot(temp_output)
35+
crop_image(temp_output, output_name)
4736

4837

49-
# Main process
5038
def main():
5139
cookie = os.getenv('COOKIE')
52-
if not cookie:
53-
raise ValueError('COOKIE environment variable not set')
5440
driver = setup_driver(cookie)
5541
try:
5642
take_screenshot(
57-
driver, 'https://adventofcode.com/2024', 'body > main > pre', 'aoc-screenshot.png'
43+
driver,
44+
'https://adventofcode.com/2024',
45+
'body > main > pre',
46+
'aoc-screenshot.png',
5847
)
5948
finally:
6049
driver.quit()

0 commit comments

Comments
 (0)