brucever commited on
Commit
7feb681
·
verified ·
1 Parent(s): 2471c51

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -32
app.py CHANGED
@@ -5,6 +5,7 @@ from selenium.webdriver.chrome.service import Service
5
  from selenium.webdriver.common.by import By
6
  from selenium.webdriver.common.keys import Keys
7
  import time
 
8
 
9
  # 全局变量
10
  driver = None
@@ -14,28 +15,37 @@ def start_browser():
14
  if driver is not None:
15
  return "⚠️ 浏览器已经在运行了!"
16
 
17
- # --- 1. 设置浏览器路径 (指向我们手下载CFT) ---
 
 
 
 
 
 
 
 
 
18
  options = Options()
19
- options.binary_location = "/opt/chrome-linux64/chrome"
 
 
20
 
21
- # --- 2. 必备参数 ---
22
- options.add_argument('--headless')
23
- options.add_argument('--no-sandbox')
24
- options.add_argument('--disable-dev-shm-usage')
25
  options.add_argument('--disable-gpu')
26
- options.add_argument('--window-size=1920,1080')
27
- options.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36')
28
 
29
  try:
30
- # --- 3. 设置驱动路径 (指向我们手动下载的 Driver) ---
31
- service = Service(executable_path="/opt/chromedriver-linux64/chromedriver")
32
 
33
  driver = webdriver.Chrome(service=service, options=options)
34
- return "✅ 浏览器启动成功!(Version Locked 121)"
35
  except Exception as e:
36
- return f"❌ 启动失败: {str(e)}"
37
 
38
- # 下面的操作逻辑不变
39
  def navigate(url):
40
  if not driver: return "请先启动浏览器"
41
  try:
@@ -48,12 +58,10 @@ def navigate(url):
48
  def send_text(selector, text):
49
  if not driver: return "请先启动浏览器"
50
  try:
51
- # 智能查找:先试CSS选择器,不行就直接发给当前焦点元素
52
  try:
53
  elem = driver.find_element(By.CSS_SELECTOR, selector)
54
  except:
55
  elem = driver.switch_to.active_element
56
-
57
  elem.clear()
58
  elem.send_keys(text)
59
  return f"已输入: {text}"
@@ -83,38 +91,30 @@ def get_screenshot():
83
  driver.save_screenshot(path)
84
  return path
85
 
86
- # --- 界面 ---
87
  with gr.Blocks(title="HF 浏览器遥控器") as demo:
88
- gr.Markdown("## 🚀 最终修复浏览器控制台")
89
-
90
  with gr.Row():
91
  start_btn = gr.Button("1. 启动浏览器", variant="primary")
92
- status_box = gr.Textbox(label="状态日志")
93
-
94
  with gr.Row():
95
- url_input = gr.Textbox(label="网址 URL", placeholder="https://...")
96
  nav_btn = gr.Button("2. 访问网址")
97
-
98
  with gr.Row():
99
  with gr.Column(scale=2):
100
  img_output = gr.Image(label="实时画面", height=600)
101
  refresh_btn = gr.Button("📸 刷新截图", variant="secondary")
102
-
103
  with gr.Column(scale=1):
104
- gr.Markdown("### 🎮 操作区")
105
- text_input = gr.Textbox(label="输入内容 (账号/密码/2FA)")
106
- type_btn = gr.Button("输入文本 (自动聚焦)")
107
- enter_btn = gr.Button("Press Enter (回车)")
108
- selector_input = gr.Textbox(label="CSS选择器 (可选)", value="input")
109
  click_btn = gr.Button("点击元素")
110
 
111
  start_btn.click(start_browser, outputs=status_box)
112
  nav_btn.click(navigate, inputs=url_input, outputs=status_box)
113
  refresh_btn.click(get_screenshot, outputs=img_output)
114
-
115
- # 输入逻辑微调:先尝试发给 selector,失败则发给 active element
116
- type_btn.click(send_text, inputs=[selector_input, text_input], outputs=status_box)
117
  enter_btn.click(send_enter, outputs=status_box)
118
- click_btn.click(click_element, inputs=selector_input, outputs=status_box)
119
 
120
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
5
  from selenium.webdriver.common.by import By
6
  from selenium.webdriver.common.keys import Keys
7
  import time
8
+ import shutil # 用来自动寻找驱动路径
9
 
10
  # 全局变量
11
  driver = None
 
15
  if driver is not None:
16
  return "⚠️ 浏览器已经在运行了!"
17
 
18
+ # --- 核心修复:自寻找系统里驱动 ---
19
+ # 这行代码会自动去找 apt-get 安装的 chromedriver 在哪
20
+ driver_path = shutil.which("chromedriver")
21
+
22
+ # 如果找不到,尝试硬编码路径
23
+ if driver_path is None:
24
+ driver_path = "/usr/bin/chromedriver"
25
+
26
+ status_msg = f"正在尝试启动... 检测到驱动路径: {driver_path}\n"
27
+
28
  options = Options()
29
+ # 同样自动寻找 chromium 浏览器路径
30
+ binary_path = shutil.which("chromium") or "/usr/bin/chromium"
31
+ options.binary_location = binary_path
32
 
33
+ options.add_argument('--headless')
34
+ options.add_argument('--no-sandbox')
35
+ options.add_argument('--disable-dev-shm-usage')
 
36
  options.add_argument('--disable-gpu')
37
+ 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")
 
38
 
39
  try:
40
+ # 使用 Selenium 4.9.1 的标准写法,强行指定路径
41
+ service = Service(executable_path=driver_path)
42
 
43
  driver = webdriver.Chrome(service=service, options=options)
44
+ return status_msg + "✅ 浏览器启动成功!(使用的是系统自带驱动,非下载版)"
45
  except Exception as e:
46
+ return status_msg + f"❌ 启动失败: {str(e)}"
47
 
48
+ # --- 下面的代码不变 ---
49
  def navigate(url):
50
  if not driver: return "请先启动浏览器"
51
  try:
 
58
  def send_text(selector, text):
59
  if not driver: return "请先启动浏览器"
60
  try:
 
61
  try:
62
  elem = driver.find_element(By.CSS_SELECTOR, selector)
63
  except:
64
  elem = driver.switch_to.active_element
 
65
  elem.clear()
66
  elem.send_keys(text)
67
  return f"已输入: {text}"
 
91
  driver.save_screenshot(path)
92
  return path
93
 
 
94
  with gr.Blocks(title="HF 浏览器遥控器") as demo:
95
+ gr.Markdown("## 🚀 最终稳定版控制台 (Selenium 4.9.1)")
 
96
  with gr.Row():
97
  start_btn = gr.Button("1. 启动浏览器", variant="primary")
98
+ status_box = gr.Textbox(label="日志")
 
99
  with gr.Row():
100
+ url_input = gr.Textbox(label="网址 URL")
101
  nav_btn = gr.Button("2. 访问网址")
 
102
  with gr.Row():
103
  with gr.Column(scale=2):
104
  img_output = gr.Image(label="实时画面", height=600)
105
  refresh_btn = gr.Button("📸 刷新截图", variant="secondary")
 
106
  with gr.Column(scale=1):
107
+ text_input = gr.Textbox(label="输入内容")
108
+ type_btn = gr.Button("输入文本")
109
+ enter_btn = gr.Button("Press Enter")
110
+ click_selector = gr.Textbox(label="点击选择器", value="button")
 
111
  click_btn = gr.Button("点击元素")
112
 
113
  start_btn.click(start_browser, outputs=status_box)
114
  nav_btn.click(navigate, inputs=url_input, outputs=status_box)
115
  refresh_btn.click(get_screenshot, outputs=img_output)
116
+ type_btn.click(send_text, inputs=[click_selector, text_input], outputs=status_box)
 
 
117
  enter_btn.click(send_enter, outputs=status_box)
118
+ click_btn.click(click_element, inputs=click_selector, outputs=status_box)
119
 
120
  demo.launch(server_name="0.0.0.0", server_port=7860)