Spaces:
Sleeping
Sleeping
File size: 6,355 Bytes
b7cdbfd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
"""
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
) |