Skip to content

Commit f73840c

Browse files
committed
testing actions
1 parent ed65319 commit f73840c

File tree

1 file changed

+65
-7
lines changed

1 file changed

+65
-7
lines changed

.github/scripts/take_screenshot.py

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,107 @@ def setup_driver():
1717
chrome_options.add_argument('--disable-dev-shm-usage')
1818
chrome_options.add_argument('--window-size=1920,1080')
1919

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+
2024
print("Chrome options configured, initializing driver...")
2125

2226
try:
2327
service = Service()
2428
driver = webdriver.Chrome(service=service, options=chrome_options)
29+
driver.set_page_load_timeout(30)
2530
print("Chrome driver initialized successfully")
2631
return driver
2732
except Exception as e:
2833
print(f"Failed to initialize Chrome driver: {str(e)}")
2934
raise
3035

3136

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("\nPage debugging information:")
47+
print(f"Current URL: {driver.current_url}")
48+
print(f"Page title: {driver.title}")
49+
print("\nFirst 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"\nTotal 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+
3265
def take_screenshot(driver, url, cookie_value, selector, output_name):
3366
print(f"Attempting to take screenshot of {selector} at {url}")
3467
os.makedirs('screenshots', exist_ok=True)
3568

3669
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...")
3875
driver.get("https://adventofcode.com")
3976

4077
# Set the session cookie
78+
print("Setting cookie...")
4179
driver.add_cookie({
4280
'name' : 'session',
4381
'value' : cookie_value,
4482
'domain': '.adventofcode.com'
4583
})
4684

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}")
4890
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)
5098

5199
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)
55111

56112
print("Element found, taking screenshot...")
113+
driver.execute_script("arguments[0].scrollIntoView();", element)
114+
time.sleep(2)
57115
element.screenshot(f'screenshots/{output_name}.png')
58116
print(f"Screenshot saved as screenshots/{output_name}.png")
59117

60118
except Exception as e:
61119
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
63121
raise
64122

65123

0 commit comments

Comments
 (0)