Browser-Use-mcp / ui /tabs_basic.py
diamond-in's picture
Update ui/tabs_basic.py
b7cdbfd verified
"""
Basic feature tabs for the UI
"""
import gradio as gr
from browser.actions import browse_and_extract, screenshot, click, fill, submit_form, execute_js
from browser.driver import close_persistent_driver
from features.extraction import get_html_source, save_html_to_file, get_page_info
from features.cookies import get_cookies, set_cookies
from features.advanced import wait_for_element, scroll_page, hover_element, press_key
def create_basic_tabs():
"""Create tabs for basic browser features"""
with gr.Tab("πŸ” Extract Text"):
with gr.Row():
url_in = gr.Textbox(label="URL", value="https://example.com")
sel_in = gr.Textbox(label="CSS Selector", value="body")
persist_check = gr.Checkbox(label="Use Persistent Browser", value=False)
out = gr.Textbox(label="Extracted Text", lines=10)
gr.Button("Extract", variant="primary").click(
browse_and_extract, [url_in, sel_in, persist_check], out
)
with gr.Tab("πŸ“Έ Screenshot"):
with gr.Row():
url_in2 = gr.Textbox(label="URL", value="https://example.com")
full_page_check = gr.Checkbox(label="Full Page Screenshot", value=False)
persist_check2 = gr.Checkbox(label="Use Persistent Browser", value=False)
out_img = gr.Image(label="Screenshot")
gr.Button("Take Screenshot", variant="primary").click(
screenshot, [url_in2, full_page_check, persist_check2], out_img
)
with gr.Tab("πŸ‘† Click"):
with gr.Row():
url_in3 = gr.Textbox(label="URL")
sel_in3 = gr.Textbox(label="CSS Selector", placeholder="button#submit")
persist_check3 = gr.Checkbox(label="Use Persistent Browser", value=False)
out3 = gr.Textbox(label="Result", lines=4)
gr.Button("Click", variant="primary").click(
click, [url_in3, sel_in3, persist_check3], out3
)
with gr.Tab("✏️ Fill"):
with gr.Row():
url_in4 = gr.Textbox(label="URL")
sel_in4 = gr.Textbox(label="CSS Selector", placeholder="input#username")
txt_in4 = gr.Textbox(label="Text to Fill")
persist_check4 = gr.Checkbox(label="Use Persistent Browser", value=False)
out4 = gr.Textbox(label="Result", lines=2)
gr.Button("Fill", variant="primary").click(
fill, [url_in4, sel_in4, txt_in4, persist_check4], out4
)
with gr.Tab("πŸ“ Form Submit"):
with gr.Row():
url_in_form = gr.Textbox(label="URL")
persist_check_form = gr.Checkbox(label="Use Persistent Browser", value=False)
form_data_in = gr.Textbox(
label="Form Data",
placeholder="selector1:value1;selector2:value2",
lines=3,
info="Format: CSS_selector:value pairs separated by semicolons"
)
submit_btn_in = gr.Textbox(
label="Submit Button Selector (optional)",
placeholder="button[type='submit']"
)
out_form = gr.Textbox(label="Result", lines=4)
gr.Button("Submit Form", variant="primary").click(
submit_form,
[url_in_form, form_data_in, submit_btn_in, persist_check_form],
out_form
)
with gr.Tab("πŸ’» Execute JS"):
with gr.Row():
url_in5 = gr.Textbox(label="URL", value="https://example.com")
persist_check5 = gr.Checkbox(label="Use Persistent Browser", value=False)
script_in5 = gr.Textbox(
label="JavaScript Code",
lines=5,
value="return {title: document.title, url: document.URL, links: document.links.length};"
)
out5 = gr.Textbox(label="Result", lines=3)
gr.Button("Execute", variant="primary").click(
execute_js, [url_in5, script_in5, persist_check5], out5
)
with gr.Tab("πŸͺ Cookies"):
with gr.Row():
with gr.Column():
gr.Markdown("### Get Cookies")
url_in_cookies = gr.Textbox(label="URL")
persist_check_cookies = gr.Checkbox(label="Use Persistent Browser", value=False)
cookies_out = gr.Textbox(label="Cookies (JSON)", lines=10)
gr.Button("Get Cookies").click(
get_cookies, [url_in_cookies, persist_check_cookies], cookies_out
)
with gr.Column():
gr.Markdown("### Set Cookies")
url_in_set_cookies = gr.Textbox(label="URL")
cookies_in = gr.Textbox(
label="Cookies JSON",
lines=8,
placeholder='[{"name": "cookie_name", "value": "cookie_value"}]'
)
persist_check_set_cookies = gr.Checkbox(label="Use Persistent Browser", value=False)
set_cookies_out = gr.Textbox(label="Result")
gr.Button("Set Cookies").click(
set_cookies,
[url_in_set_cookies, cookies_in, persist_check_set_cookies],
set_cookies_out
)
with gr.Tab("πŸ“„ HTML Source"):
with gr.Row():
url_in_html = gr.Textbox(label="URL", value="https://example.com")
persist_check_html = gr.Checkbox(label="Use Persistent Browser", value=False)
with gr.Row():
with gr.Column():
out_html = gr.Code(label="HTML Source Code", language="html", lines=20)
gr.Button("Get HTML", variant="primary").click(
get_html_source, [url_in_html, persist_check_html], out_html
)
with gr.Column():
filename_in = gr.Textbox(label="Filename (optional)", placeholder="auto-generated")
save_out = gr.File(label="Saved HTML File")
gr.Button("Save HTML to File", variant="secondary").click(
save_html_to_file,
[url_in_html, filename_in, persist_check_html],
save_out
)
with gr.Tab("πŸ”§ Browser Control"):
close_out = gr.Textbox(label="Status")
gr.Button("Close Persistent Browser", variant="stop").click(
close_persistent_driver, [], close_out
)