|
import time |
|
from io import BytesIO |
|
|
|
import gradio as gr |
|
from PIL import Image |
|
from selenium import webdriver |
|
from selenium.common.exceptions import WebDriverException |
|
|
|
import main_script |
|
|
|
driver = None |
|
|
|
|
|
def get_chrome_options(): |
|
options = webdriver.ChromeOptions() |
|
options.add_argument('--headless') |
|
options.add_argument('--no-sandbox') |
|
options.add_argument('--disable-dev-shm-usage') |
|
|
|
return options |
|
|
|
|
|
def set_driver(): |
|
options = get_chrome_options() |
|
|
|
try: |
|
web_driver = webdriver.Chrome(options=options) |
|
web_driver.set_window_size(1080, 720) |
|
except WebDriverException as e: |
|
return Image.new('RGB', (1, 1)) |
|
|
|
return web_driver |
|
|
|
|
|
def take_screenshot(url): |
|
global driver |
|
|
|
try: |
|
driver = set_driver() |
|
driver.get(url) |
|
driver.implicitly_wait(10) |
|
|
|
images = [] |
|
|
|
for i in range(3): |
|
png = driver.get_screenshot_as_png() |
|
image = Image.open(BytesIO(png)) |
|
images.append(image) |
|
driver.execute_script("window.scrollBy(0, window.innerHeight);") |
|
time.sleep(1) |
|
|
|
|
|
except WebDriverException as e: |
|
return Image.new('RGB', (1, 1)) |
|
finally: |
|
if driver: |
|
driver.quit() |
|
|
|
return images |
|
|
|
def call_main_script(): |
|
main_script.main() |
|
|
|
def main(url): |
|
if url== "amit": |
|
return call_main_script() |
|
else: |
|
return take_screenshot(url) |
|
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
fn=main, |
|
inputs=gr.Textbox(label="Website URL", value="https://www.google.com/"), |
|
outputs=gr.Gallery(label="Screenshots", columns=3, height="auto"), |
|
title="Website Screenshots", |
|
description="Take multiple screenshots of a website using Selenium in a Gradio Space!" |
|
) |
|
|
|
iface.launch() |
|
|