1
+ from selenium import webdriver
2
+ from selenium .webdriver .chrome .service import Service
3
+ from selenium .webdriver .common .by import By
4
+ from selenium .webdriver .support .ui import WebDriverWait
5
+ from selenium .webdriver .support import expected_conditions as EC
1
6
import os
2
7
3
- cookie = os .environ ['COOKIE' ]
4
8
5
- print (f'cookie: { cookie :10} ' )
9
+ def setup_driver ():
10
+ chrome_options = webdriver .ChromeOptions ()
11
+ chrome_options .add_argument ('--headless' )
12
+ chrome_options .add_argument ('--no-sandbox' )
13
+ chrome_options .add_argument ('--disable-dev-shm-usage' )
14
+
15
+ service = Service ()
16
+ driver = webdriver .Chrome (service = service , options = chrome_options )
17
+ return driver
18
+
19
+
20
+ def take_element_screenshot (driver , url , selector , output_name ):
21
+ # Create screenshots directory if it doesn't exist
22
+ os .makedirs ('screenshots' , exist_ok = True )
23
+
24
+ try :
25
+ driver .get (url )
26
+
27
+ # Wait for element to be present and visible
28
+ element = WebDriverWait (driver , 10 ).until (
29
+ EC .presence_of_element_located ((By .CSS_SELECTOR , selector ))
30
+ )
31
+
32
+ # Ensure element is visible in viewport
33
+ driver .execute_script ("arguments[0].scrollIntoView();" , element )
34
+
35
+ # Take screenshot of specific element
36
+ element .screenshot (f'screenshots/{ output_name } .png' )
37
+ print (f"Screenshot saved as screenshots/{ output_name } .png" )
38
+
39
+ except Exception as e :
40
+ print (f"Error taking screenshot: { str (e )} " )
41
+
42
+
43
+ def main ():
44
+ driver = setup_driver ()
45
+ try :
46
+ # Example usage - replace with your target webpage and element selector
47
+ take_element_screenshot (
48
+ driver = driver ,
49
+ url = 'https://github.com' ,
50
+ selector = '.header' , # Example: capturing GitHub header
51
+ output_name = 'github-header'
52
+ )
53
+ finally :
54
+ driver .quit ()
6
55
7
- from selenium import webdriver
8
- from selenium .webdriver .chrome .service import Service
9
- from selenium .webdriver .chrome .options import Options
10
- from PIL import Image
11
- from io import BytesIO
12
- import os
13
56
14
- # Configure Chrome options
15
- options = Options ()
16
- options .headless = True # Run in headless mode (no GUI)
17
- options .add_argument ("--disable-gpu" )
18
- options .add_argument ("--no-sandbox" )
19
- options .add_argument ("--disable-dev-shm-usage" )
20
-
21
- # Path to ChromeDriver (ensure this is set up in your environment)
22
- CHROMEDRIVER_PATH = os .getenv ("CHROMEDRIVER_PATH" , "/usr/local/bin/chromedriver" )
23
-
24
- # Initialize WebDriver
25
- service = Service (executable_path = CHROMEDRIVER_PATH )
26
- driver = webdriver .Chrome (service = service , options = options )
27
-
28
- try :
29
- # Navigate to the target URL
30
- TARGET_URL = os .getenv ("TARGET_URL" , "http://stackoverflow.com/" )
31
- driver .get (TARGET_URL )
32
-
33
- # Locate the element by its ID
34
- element = driver .find_element ("id" , "hlogo" ) # Replace 'hlogo' with the actual element ID
35
-
36
- # Get the element's location and size
37
- location = element .location
38
- size = element .size
39
-
40
- # Take a screenshot of the entire page
41
- png = driver .get_screenshot_as_png ()
42
-
43
- # Open the image using PIL
44
- im = Image .open (BytesIO (png ))
45
-
46
- # Define crop points
47
- left = location ['x' ]
48
- top = location ['y' ]
49
- right = location ['x' ] + size ['width' ]
50
- bottom = location ['y' ] + size ['height' ]
51
-
52
- # Crop the image to the element and save it
53
- im = im .crop ((left , top , right , bottom ))
54
- screenshot_path = "screenshot.png"
55
- im .save (screenshot_path )
56
- print (f"Screenshot saved to { screenshot_path } " )
57
-
58
- except Exception as e :
59
- print (f"An error occurred: { e } " )
60
- finally :
61
- # Quit the WebDriver
62
- driver .quit ()
63
- print ("Driver closed" )
57
+ if __name__ == "__main__" :
58
+ main ()
0 commit comments