Skip to content

Commit

Permalink
testing actions
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubdotpy3d committed Jan 11, 2025
1 parent 8343349 commit 1645b96
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 38 deletions.
83 changes: 52 additions & 31 deletions .github/scripts/take_screenshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,60 @@

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

from io import BytesIO

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from PIL import Image
from io import BytesIO
import os
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service

# Get configurations from environment variables (with defaults)
GECKODRIVER_PATH = os.getenv("GECKODRIVER_PATH", "/usr/local/bin/geckodriver") # Default for Linux pipelines

# Configure Chrome options
options = Options()
options.headless = True # Run in headless mode (no GUI)
service = Service(executable_path=GECKODRIVER_PATH)
fox = webdriver.Firefox(service=service, options=options)

fox.get('http://stackoverflow.com/')


# now that we have the preliminary stuff out of the way time to get that image :D
element = fox.find_element_by_id('hlogo') # find part of the page you want image of
location = element.location
size = element.size
png = fox.get_screenshot_as_png() # saves screenshot of entire page
fox.quit()

im = Image.open(BytesIO(png)) # uses PIL library to open image in memory

left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']


im = im.crop((left, top, right, bottom)) # defines crop points
im.save('screenshot.png') # saves new cropped image
options.add_argument("--disable-gpu")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")

# Path to ChromeDriver (ensure this is set up in your environment)
CHROMEDRIVER_PATH = os.getenv("CHROMEDRIVER_PATH", "/usr/local/bin/chromedriver")

# Initialize WebDriver
service = Service(executable_path=CHROMEDRIVER_PATH)
driver = webdriver.Chrome(service=service, options=options)

try:
# Navigate to the target URL
TARGET_URL = os.getenv("TARGET_URL", "http://stackoverflow.com/")
driver.get(TARGET_URL)

# Locate the element by its ID
element = driver.find_element("id", "hlogo") # Replace 'hlogo' with the actual element ID

# Get the element's location and size
location = element.location
size = element.size

# Take a screenshot of the entire page
png = driver.get_screenshot_as_png()

# Open the image using PIL
im = Image.open(BytesIO(png))

# Define crop points
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']

# Crop the image to the element and save it
im = im.crop((left, top, right, bottom))
screenshot_path = "screenshot.png"
im.save(screenshot_path)
print(f"Screenshot saved to {screenshot_path}")

except Exception as e:
print(f"An error occurred: {e}")
finally:
# Quit the WebDriver
driver.quit()
print("Driver closed")
12 changes: 5 additions & 7 deletions .github/workflows/update_readme.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ jobs:
- name: Install the project
run: uv sync --group actions --all-extras --dev

- name: Install Firefox
run: sudo apt-get update && sudo apt-get install -y firefox

- name: Install GeckoDriver
- name: setup chrome driver
uses: nanasess/setup-chromedriver@v2
run: |
GECKODRIVER_VERSION=$(curl -s https://api.github.com/repos/mozilla/geckodriver/releases/latest | jq -r ".tag_name")
curl -L "https://github.com/mozilla/geckodriver/releases/download/$GECKODRIVER_VERSION/geckodriver-$GECKODRIVER_VERSION-linux64.tar.gz" | tar xz
sudo mv geckodriver /usr/local/bin/
export DISPLAY=:99
chromedriver --url-base=/wd/hub &
sudo Xvfb -ac :99 -screen 0 1280x1024x24 > /dev/null 2>&1 & # optional
- name: Take Screenshot
env:
Expand Down

0 comments on commit 1645b96

Please sign in to comment.