diamond-in commited on
Commit
906e5a7
·
verified ·
1 Parent(s): a63ba40

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -0
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from selenium import webdriver
3
+ from selenium.webdriver.chrome.service import Service
4
+ from selenium.webdriver.chrome.options import Options
5
+ from selenium.webdriver.common.by import By
6
+ from webdriver_manager.chrome import ChromeDriverManager
7
+ import time
8
+
9
+ def _make_driver():
10
+ chrome_options = Options()
11
+ chrome_options.add_argument("--headless")
12
+ chrome_options.add_argument("--no-sandbox")
13
+ chrome_options.add_argument("--disable-dev-shm-usage")
14
+ driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
15
+ driver.set_page_load_timeout(15)
16
+ return driver
17
+
18
+ def browse_and_extract(url: str, selector: str = "body") -> str:
19
+ """Open a webpage and extract text of a CSS selector."""
20
+ try:
21
+ driver = _make_driver()
22
+ driver.get(url)
23
+ elem = driver.find_element(By.CSS_SELECTOR, selector)
24
+ text = elem.text
25
+ driver.quit()
26
+ return text
27
+ except Exception as e:
28
+ return f"Error: {e}"
29
+
30
+ def screenshot(url: str) -> str:
31
+ """Take a screenshot of a webpage."""
32
+ try:
33
+ driver = _make_driver()
34
+ driver.get(url)
35
+ path = "/tmp/screenshot.png"
36
+ driver.save_screenshot(path)
37
+ driver.quit()
38
+ return path
39
+ except Exception as e:
40
+ return f"Error: {e}"
41
+
42
+ def click(url: str, selector: str) -> str:
43
+ """Open a webpage, click an element, and return the new page title."""
44
+ try:
45
+ driver = _make_driver()
46
+ driver.get(url)
47
+ elem = driver.find_element(By.CSS_SELECTOR, selector)
48
+ elem.click()
49
+ time.sleep(2) # let navigation happen
50
+ title = driver.title
51
+ driver.quit()
52
+ return f"Clicked {selector}, new title: {title}"
53
+ except Exception as e:
54
+ return f"Error: {e}"
55
+
56
+ def fill(url: str, selector: str, text: str) -> str:
57
+ """Open a webpage, fill an input box, and return page title."""
58
+ try:
59
+ driver = _make_driver()
60
+ driver.get(url)
61
+ elem = driver.find_element(By.CSS_SELECTOR, selector)
62
+ elem.clear()
63
+ elem.send_keys(text)
64
+ driver.quit()
65
+ return f"Filled {selector} with '{text}'"
66
+ except Exception as e:
67
+ return f"Error: {e}"
68
+
69
+ with gr.Blocks() as demo:
70
+ gr.Markdown("# 🌐 MCP Browser (Selenium + Chromium)")
71
+
72
+ with gr.Tab("Extract Text"):
73
+ url_in = gr.Textbox(label="URL")
74
+ sel_in = gr.Textbox(label="CSS Selector", value="body")
75
+ out = gr.Textbox(label="Extracted Text", lines=15)
76
+ gr.Button("Run").click(browse_and_extract, [url_in, sel_in], out)
77
+
78
+ with gr.Tab("Screenshot"):
79
+ url_in2 = gr.Textbox(label="URL")
80
+ out_img = gr.Image(label="Screenshot")
81
+ gr.Button("Take Screenshot").click(screenshot, url_in2, out_img)
82
+
83
+ with gr.Tab("Click"):
84
+ url_in3 = gr.Textbox(label="URL")
85
+ sel_in3 = gr.Textbox(label="CSS Selector")
86
+ out3 = gr.Textbox(label="Result")
87
+ gr.Button("Click").click(click, [url_in3, sel_in3], out3)
88
+
89
+ with gr.Tab("Fill"):
90
+ url_in4 = gr.Textbox(label="URL")
91
+ sel_in4 = gr.Textbox(label="CSS Selector")
92
+ txt_in4 = gr.Textbox(label="Text to Fill")
93
+ out4 = gr.Textbox(label="Result")
94
+ gr.Button("Fill").click(fill, [url_in4, sel_in4, txt_in4], out4)
95
+
96
+ if __name__ == "__main__":
97
+ demo.launch(mcp_server=True)