|
|
|
from selenium import webdriver |
|
|
|
import gradio as gr |
|
import uuid |
|
import re |
|
from PIL import Image |
|
from io import BytesIO |
|
from selenium.common.exceptions import WebDriverException |
|
from selenium.webdriver.common.keys import Keys |
|
from selenium.webdriver.common.by import By |
|
|
|
|
|
driver_type = 'chromedriver' |
|
driver=False |
|
def run_script(html: str, style: str,height,width): |
|
uid=uuid.uuid4() |
|
html = html.replace("\n","<br>") |
|
|
|
|
|
is_url=True |
|
if is_url: |
|
options = webdriver.ChromeOptions() |
|
options.add_argument('--headless') |
|
options.add_argument('--no-sandbox') |
|
options.add_argument('--disable-dev-shm-usage') |
|
|
|
try: |
|
|
|
url = "https://gab.ai/c/65af72173a6a44d9799e202e" |
|
driver = webdriver.Chrome(options=options) |
|
driver.get(url) |
|
|
|
driver.implicitly_wait(30) |
|
driver.set_window_size(int(width), int(height)) |
|
obj = driver.find_element(By.CLASS_NAME, "main") |
|
|
|
|
|
|
|
|
|
|
|
''' |
|
find_element(By.ID, "id") |
|
find_element(By.NAME, "name") |
|
find_element(By.XPATH, "xpath") |
|
find_element(By.LINK_TEXT, "link text") |
|
find_element(By.PARTIAL_LINK_TEXT, "partial link text") |
|
find_element(By.TAG_NAME, "tag name") |
|
find_element(By.CLASS_NAME, "class name") |
|
find_element(By.CSS_SELECTOR, "css selector") |
|
''' |
|
|
|
|
|
cookie_jar = [] |
|
|
|
|
|
screenshot = obj.screenshot(f'{uid}-tmp.png') |
|
|
|
except WebDriverException as e: |
|
return [Image.new('RGB', (1, 1)), e, None,None] |
|
finally: |
|
if driver: |
|
driver.quit() |
|
|
|
|
|
return [Image.open(f'{uid}-tmp.png'), 'operation success.',url,html] |
|
else: |
|
return [None, 'Please enter a valid URL of a website/host.',None,None] |
|
|
|
html="""<h1>This is the title</h1> |
|
This is some information |
|
bla bla |
|
• a bullet |
|
• or two |
|
""" |
|
|
|
sty="""height:500;width:1000;""" |
|
|
|
with gr.Blocks() as app: |
|
with gr.Row(): |
|
inp = gr.Textbox(label="URL",lines=1,value=html) |
|
with gr.Column(): |
|
style = gr.Textbox(label="CSS") |
|
with gr.Row(): |
|
height=gr.Number(label="Height", value=4096) |
|
width=gr.Number(label="width",value=512) |
|
btn= gr.Button() |
|
with gr.Row(): |
|
with gr.Column(): |
|
outim = gr.Image() |
|
with gr.Column(): |
|
outp = gr.HTML() |
|
cook = gr.Textbox(label="URL") |
|
html=gr.Textbox(visible=False) |
|
btn.click(run_script,[inp,style,height,width],[outim,outp,cook,html]) |
|
app.launch() |