Skip to content

Commit 1645b96

Browse files
committed
testing actions
1 parent 8343349 commit 1645b96

File tree

2 files changed

+57
-38
lines changed

2 files changed

+57
-38
lines changed

.github/scripts/take_screenshot.py

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,60 @@
44

55
print(f'cookie: {cookie:10}')
66

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
910
from PIL import Image
11+
from io import BytesIO
1012
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
1713

14+
# Configure Chrome options
1815
options = Options()
1916
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")

.github/workflows/update_readme.yml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ jobs:
1717
- name: Install the project
1818
run: uv sync --group actions --all-extras --dev
1919

20-
- name: Install Firefox
21-
run: sudo apt-get update && sudo apt-get install -y firefox
22-
23-
- name: Install GeckoDriver
20+
- name: setup chrome driver
21+
uses: nanasess/setup-chromedriver@v2
2422
run: |
25-
GECKODRIVER_VERSION=$(curl -s https://api.github.com/repos/mozilla/geckodriver/releases/latest | jq -r ".tag_name")
26-
curl -L "https://github.com/mozilla/geckodriver/releases/download/$GECKODRIVER_VERSION/geckodriver-$GECKODRIVER_VERSION-linux64.tar.gz" | tar xz
27-
sudo mv geckodriver /usr/local/bin/
23+
export DISPLAY=:99
24+
chromedriver --url-base=/wd/hub &
25+
sudo Xvfb -ac :99 -screen 0 1280x1024x24 > /dev/null 2>&1 & # optional
2826
2927
- name: Take Screenshot
3028
env:

0 commit comments

Comments
 (0)