|
| 1 | +import os |
| 2 | +import time |
1 | 3 | from selenium import webdriver
|
2 | 4 | from selenium.webdriver.chrome.service import Service
|
3 | 5 | from selenium.webdriver.common.by import By
|
4 | 6 | from selenium.webdriver.support.ui import WebDriverWait
|
5 | 7 | from selenium.webdriver.support import expected_conditions as EC
|
6 |
| -import os |
| 8 | +from selenium.webdriver.chrome.options import Options |
7 | 9 |
|
8 | 10 |
|
9 | 11 | def setup_driver():
|
10 |
| - chrome_options = webdriver.ChromeOptions() |
11 |
| - chrome_options.add_argument('--headless') |
| 12 | + print("Setting up Chrome driver...") |
| 13 | + chrome_options = Options() |
| 14 | + |
| 15 | + # Required arguments for running in GitHub Actions |
| 16 | + chrome_options.add_argument('--headless=new') # Updated headless argument |
12 | 17 | chrome_options.add_argument('--no-sandbox')
|
13 | 18 | chrome_options.add_argument('--disable-dev-shm-usage')
|
| 19 | + chrome_options.add_argument('--disable-gpu') |
| 20 | + chrome_options.add_argument('--window-size=1920,1080') |
| 21 | + chrome_options.add_argument('--disable-extensions') |
14 | 22 |
|
15 |
| - service = Service() |
16 |
| - driver = webdriver.Chrome(service=service, options=chrome_options) |
17 |
| - return driver |
| 23 | + # Additional debugging options |
| 24 | + chrome_options.add_argument('--verbose') |
| 25 | + chrome_options.add_argument('--log-level=0') |
| 26 | + |
| 27 | + print("Chrome options configured, initializing driver...") |
| 28 | + |
| 29 | + try: |
| 30 | + service = Service() |
| 31 | + driver = webdriver.Chrome(service=service, options=chrome_options) |
| 32 | + print("Chrome driver initialized successfully") |
| 33 | + return driver |
| 34 | + except Exception as e: |
| 35 | + print(f"Failed to initialize Chrome driver: {str(e)}") |
| 36 | + raise |
18 | 37 |
|
19 | 38 |
|
20 | 39 | def take_element_screenshot(driver, url, selector, output_name):
|
21 |
| - # Create screenshots directory if it doesn't exist |
| 40 | + print(f"Attempting to take screenshot of {selector} at {url}") |
22 | 41 | os.makedirs('screenshots', exist_ok=True)
|
23 | 42 |
|
24 | 43 | try:
|
| 44 | + print("Navigating to page...") |
25 | 45 | driver.get(url)
|
26 | 46 |
|
| 47 | + # Add a small delay to ensure page loads |
| 48 | + time.sleep(5) |
| 49 | + print("Page loaded, waiting for element...") |
| 50 | + |
27 | 51 | # Wait for element to be present and visible
|
28 |
| - element = WebDriverWait(driver, 10).until( |
| 52 | + element = WebDriverWait(driver, 20).until( |
29 | 53 | EC.presence_of_element_located((By.CSS_SELECTOR, selector))
|
30 | 54 | )
|
31 | 55 |
|
| 56 | + print("Element found, preparing for screenshot...") |
| 57 | + |
32 | 58 | # Ensure element is visible in viewport
|
33 | 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") |
34 | 65 |
|
35 | 66 | # Take screenshot of specific element
|
36 | 67 | element.screenshot(f'screenshots/{output_name}.png')
|
37 |
| - print(f"Screenshot saved as screenshots/{output_name}.png") |
| 68 | + print(f"Element screenshot saved as screenshots/{output_name}.png") |
38 | 69 |
|
39 | 70 | except Exception as e:
|
40 |
| - print(f"Error taking screenshot: {str(e)}") |
| 71 | + print(f"Error during screenshot process: {str(e)}") |
| 72 | + print(f"Current URL: {driver.current_url}") |
| 73 | + print(f"Page source length: {len(driver.page_source)}") |
| 74 | + raise |
41 | 75 |
|
42 | 76 |
|
43 | 77 | def main():
|
44 |
| - driver = setup_driver() |
| 78 | + print("Starting screenshot process...") |
| 79 | + driver = None |
45 | 80 | try:
|
46 |
| - # Example usage - replace with your target webpage and element selector |
| 81 | + driver = setup_driver() |
| 82 | + |
| 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 | + ) |
| 90 | + |
| 91 | + # If the above works, try your actual target |
47 | 92 | take_element_screenshot(
|
48 | 93 | driver=driver,
|
49 | 94 | url='https://github.com',
|
50 |
| - selector='.header', # Example: capturing GitHub header |
| 95 | + selector='.header', |
51 | 96 | output_name='github-header'
|
52 | 97 | )
|
| 98 | + |
| 99 | + except Exception as e: |
| 100 | + print(f"Fatal error in main: {str(e)}") |
| 101 | + raise |
53 | 102 | finally:
|
54 |
| - driver.quit() |
| 103 | + if driver: |
| 104 | + print("Cleaning up driver...") |
| 105 | + driver.quit() |
55 | 106 |
|
56 | 107 |
|
57 | 108 | if __name__ == "__main__":
|
|
0 commit comments