|
4 | 4 |
|
5 | 5 | print(f'cookie: {cookie:10}')
|
6 | 6 |
|
7 |
| -from io import BytesIO |
8 |
| - |
| 7 | +from selenium import webdriver |
| 8 | +from selenium.webdriver.chrome.service import Service |
| 9 | +from selenium.webdriver.chrome.options import Options |
9 | 10 | from PIL import Image
|
| 11 | +from io import BytesIO |
10 | 12 | import os
|
11 |
| -from selenium import webdriver |
12 |
| -from selenium.webdriver.firefox.options import Options |
13 |
| -from selenium.webdriver.firefox.service import Service |
14 |
| - |
15 |
| -# Get configurations from environment variables (with defaults) |
16 |
| -GECKODRIVER_PATH = os.getenv("GECKODRIVER_PATH", "/usr/local/bin/geckodriver") # Default for Linux pipelines |
17 | 13 |
|
| 14 | +# Configure Chrome options |
18 | 15 | options = Options()
|
19 | 16 | options.headless = True # Run in headless mode (no GUI)
|
20 |
| -service = Service(executable_path=GECKODRIVER_PATH) |
21 |
| -fox = webdriver.Firefox(service=service, options=options) |
22 |
| - |
23 |
| -fox.get('http://stackoverflow.com/') |
24 |
| - |
25 |
| - |
26 |
| -# now that we have the preliminary stuff out of the way time to get that image :D |
27 |
| -element = fox.find_element_by_id('hlogo') # find part of the page you want image of |
28 |
| -location = element.location |
29 |
| -size = element.size |
30 |
| -png = fox.get_screenshot_as_png() # saves screenshot of entire page |
31 |
| -fox.quit() |
32 |
| - |
33 |
| -im = Image.open(BytesIO(png)) # uses PIL library to open image in memory |
34 |
| - |
35 |
| -left = location['x'] |
36 |
| -top = location['y'] |
37 |
| -right = location['x'] + size['width'] |
38 |
| -bottom = location['y'] + size['height'] |
39 |
| - |
40 |
| - |
41 |
| -im = im.crop((left, top, right, bottom)) # defines crop points |
42 |
| -im.save('screenshot.png') # saves new cropped image |
| 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") |
0 commit comments