import gradio as gr from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys import time import shutil # 用来自动寻找驱动路径 # 全局变量 driver = None def start_browser(): global driver if driver is not None: return "⚠️ 浏览器已经在运行了!" # --- 核心修复:自动寻找系统里的驱动 --- # 这行代码会自动去找 apt-get 安装的 chromedriver 在哪 driver_path = shutil.which("chromedriver") # 如果找不到,尝试硬编码路径 if driver_path is None: driver_path = "/usr/bin/chromedriver" status_msg = f"正在尝试启动... 检测到驱动路径: {driver_path}\n" options = Options() # 同样自动寻找 chromium 浏览器路径 binary_path = shutil.which("chromium") or "/usr/bin/chromium" options.binary_location = binary_path options.add_argument('--headless') options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') options.add_argument('--disable-gpu') options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36") try: # 使用 Selenium 4.9.1 的标准写法,强行指定路径 service = Service(executable_path=driver_path) driver = webdriver.Chrome(service=service, options=options) return status_msg + "✅ 浏览器启动成功!(使用的是系统自带驱动,非下载版)" except Exception as e: return status_msg + f"❌ 启动失败: {str(e)}" # --- 下面的代码不变 --- def navigate(url): if not driver: return "请先启动浏览器" try: driver.get(url) time.sleep(3) return f"已访问: {url}" except Exception as e: return f"访问出错: {str(e)}" def send_text(selector, text): if not driver: return "请先启动浏览器" try: try: elem = driver.find_element(By.CSS_SELECTOR, selector) except: elem = driver.switch_to.active_element elem.clear() elem.send_keys(text) return f"已输入: {text}" except Exception as e: return f"输入失败: {str(e)}" def click_element(selector): if not driver: return "请先启动浏览器" try: elem = driver.find_element(By.CSS_SELECTOR, selector) elem.click() return "点击成功" except Exception as e: return f"点击失败: {str(e)}" def send_enter(): if not driver: return "请先启动浏览器" try: driver.switch_to.active_element.send_keys(Keys.ENTER) return "已按下回车键" except Exception as e: return f"操作失败: {str(e)}" def get_screenshot(): if not driver: return None path = "/tmp/screenshot.png" driver.save_screenshot(path) return path with gr.Blocks(title="HF 浏览器遥控器") as demo: gr.Markdown("## 🚀 最终稳定版控制台 (Selenium 4.9.1)") with gr.Row(): start_btn = gr.Button("1. 启动浏览器", variant="primary") status_box = gr.Textbox(label="日志") with gr.Row(): url_input = gr.Textbox(label="网址 URL") nav_btn = gr.Button("2. 访问网址") with gr.Row(): with gr.Column(scale=2): img_output = gr.Image(label="实时画面", height=600) refresh_btn = gr.Button("📸 刷新截图", variant="secondary") with gr.Column(scale=1): text_input = gr.Textbox(label="输入内容") type_btn = gr.Button("输入文本") enter_btn = gr.Button("Press Enter") click_selector = gr.Textbox(label="点击选择器", value="button") click_btn = gr.Button("点击元素") start_btn.click(start_browser, outputs=status_box) nav_btn.click(navigate, inputs=url_input, outputs=status_box) refresh_btn.click(get_screenshot, outputs=img_output) type_btn.click(send_text, inputs=[click_selector, text_input], outputs=status_box) enter_btn.click(send_enter, outputs=status_box) click_btn.click(click_element, inputs=click_selector, outputs=status_box) demo.launch(server_name="0.0.0.0", server_port=7860)