Skip to content

Commit be16337

Browse files
committed
testing actions
1 parent 2ec7e42 commit be16337

File tree

2 files changed

+66
-59
lines changed

2 files changed

+66
-59
lines changed

.github/scripts/take_screenshot.py

Lines changed: 53 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,58 @@
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
16
import os
27

3-
cookie = os.environ['COOKIE']
48

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()
655

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
1356

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()

.github/workflows/update_readme.yml

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

20-
- name: setup chrome driver
20+
- name: Install Chrome
21+
uses: browser-actions/setup-chrome@v1
22+
with:
23+
chrome-version: stable
24+
25+
- name: Install Chrome WebDriver
2126
uses: nanasess/setup-chromedriver@v2
2227

2328
- name: Take Screenshot
@@ -26,6 +31,12 @@ jobs:
2631
run: |
2732
uv run .github/scripts/take_screenshot.py
2833
34+
- name: Upload screenshot artifact
35+
uses: actions/upload-artifact@v4
36+
with:
37+
name: webpage-screenshots
38+
path: screenshots/
39+
2940
- name: Update README
3041
run: |
3142
uv run .github/scripts/update_readme.py
@@ -37,3 +48,4 @@ jobs:
3748
# git add README.md screenshot.png
3849
# git commit -m "Update screenshot in README"
3950
# git push
51+

0 commit comments

Comments
 (0)