allcode_by_thrtest.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. import time
  2. import tkinter as tk
  3. from tkinter import ttk, filedialog, messagebox
  4. import json
  5. import os
  6. import threading
  7. import logging
  8. from selenium import webdriver
  9. from selenium.webdriver.common.by import By
  10. from selenium.webdriver.chrome.service import Service
  11. from selenium.webdriver.support.ui import Select
  12. from selenium.webdriver.support import expected_conditions as EC
  13. from selenium.webdriver.support.ui import WebDriverWait
  14. from selenium.common.exceptions import TimeoutException, ElementClickInterceptedException
  15. from utils import abbreviation_to_index
  16. from bit_api import *
  17. from concurrent.futures import ThreadPoolExecutor, as_completed
  18. import datetime
  19. from enum import Enum
  20. current_time = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
  21. # 设置日志
  22. logging.basicConfig(level=logging.INFO)
  23. logger = logging.getLogger(__name__)
  24. result_logger = logging.getLogger("normal")
  25. # 结果保留日志
  26. logger1 = logging.getLogger('result_log')
  27. logger1.setLevel(logging.INFO)
  28. handler1 = logging.FileHandler("result_log" + ".log")
  29. formatter1 = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
  30. handler1.setFormatter(formatter1)
  31. logger1.addHandler(handler1)
  32. # 自定义日志处理器,用于将日志输出到 GUI 的文本框
  33. class TextHandler(logging.Handler):
  34. def __init__(self, text_widget, adjust_height_callback):
  35. super().__init__()
  36. self.text_widget = text_widget
  37. self.adjust_height_callback = adjust_height_callback
  38. def emit(self, record):
  39. msg = self.format(record)
  40. self.text_widget.insert(tk.END, msg + '\n')
  41. self.text_widget.see(tk.END)
  42. self.adjust_height_callback()
  43. # GUI 类
  44. class AdsDataGUI:
  45. def __init__(self, root):
  46. self.root = root
  47. self.root.title("自动化注册 - BitBrowser")
  48. # 初始化运行状态
  49. self.running = False
  50. self.current_web = None # 用于存储当前 Selenium WebDriver 实例
  51. # 加载保存的配置
  52. self.config_file = "config_th.json"
  53. self.load_config()
  54. # 参数输入框
  55. self.params = {
  56. "WINDOW_ID": tk.StringVar(value=self.config.get("WINDOW_ID", "5c2216ed3b3e4bf0bc144b5ad0c87a5b")),
  57. "BASE_URL": tk.StringVar(value=self.config.get("BASE_URL", "http://127.0.0.1:54345")),
  58. "API_KEY": tk.StringVar(value=self.config.get("API_KEY", "704daf420f7244d08be51f61c987a232")),
  59. "FILE_PATH": tk.StringVar(value=self.config.get("FILE_PATH", "generated_data.txt")),
  60. "AMOUNT": tk.StringVar(value=self.config.get("AMOUNT", "$501-$1,000")),
  61. "SLEEP_TIME_1": tk.DoubleVar(value=self.config.get("SLEEP_TIME_1", 60.0)), # 验证码等待时间,强制设置为 60 秒
  62. "SLEEP_TIME_2": tk.DoubleVar(value=self.config.get("SLEEP_TIME_2", 3.0)), # 提交后等待时间
  63. "SLEEP_TIME_3": tk.DoubleVar(value=self.config.get("SLEEP_TIME_3", 10.0)), # 关闭后等待时间
  64. "NO_SANDBOX": tk.BooleanVar(value=self.config.get("NO_SANDBOX", True)),
  65. "DISABLE_DEV_SHM": tk.BooleanVar(value=self.config.get("DISABLE_DEV_SHM", True)),
  66. "START_MAXIMIZED": tk.BooleanVar(value=self.config.get("START_MAXIMIZED", True))
  67. }
  68. # 创建主框架
  69. self.main_frame = ttk.Frame(self.root)
  70. self.main_frame.pack(fill=tk.BOTH, expand=True)
  71. # 创建画布和滚动条
  72. self.canvas = tk.Canvas(self.main_frame)
  73. self.scrollbar = ttk.Scrollbar(self.main_frame, orient=tk.VERTICAL, command=self.canvas.yview)
  74. self.scrollable_frame = ttk.Frame(self.canvas)
  75. self.scrollable_frame.bind(
  76. "<Configure>",
  77. lambda e: self.canvas.configure(scrollregion=self.canvas.bbox("all"))
  78. )
  79. self.canvas.create_window((0, 0), window=self.scrollable_frame, anchor="nw")
  80. self.canvas.configure(yscrollcommand=self.scrollbar.set)
  81. self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
  82. self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  83. # 创建 GUI 元素(中文界面)
  84. row = 0
  85. tk.Label(self.scrollable_frame, text="自动化注册 - BitBrowser", font=("Arial", 16)).grid(row=row, column=0,
  86. columnspan=3, pady=10)
  87. row += 1
  88. # 参数输入框
  89. labels = [
  90. ("窗口 ID:", self.params["WINDOW_ID"]),
  91. ("API 地址:", self.params["BASE_URL"]),
  92. ("API 密钥:", self.params["API_KEY"]),
  93. ("文件路径:", self.params["FILE_PATH"]),
  94. ("金额范围:", self.params["AMOUNT"]),
  95. ("等待时间 1 (验证码, 秒):", self.params["SLEEP_TIME_1"]),
  96. ("等待时间 2 (提交后, 秒):", self.params["SLEEP_TIME_2"]),
  97. ("等待时间 3 (关闭后, 秒):", self.params["SLEEP_TIME_3"])
  98. ]
  99. for label_text, var in labels:
  100. tk.Label(self.scrollable_frame, text=label_text).grid(row=row, column=0, sticky="e", padx=5, pady=5)
  101. if label_text == "金额范围:":
  102. amount_options = ["$1-$500", "$501-$1,000", "$1,001-$5,000", "超过 $5,000"]
  103. ttk.Combobox(self.scrollable_frame, textvariable=var, values=amount_options, state="readonly").grid(
  104. row=row, column=1, padx=5, pady=5, sticky="ew")
  105. else:
  106. tk.Entry(self.scrollable_frame, textvariable=var).grid(row=row, column=1, padx=5, pady=5, sticky="ew")
  107. if label_text == "文件路径:":
  108. tk.Button(self.scrollable_frame, text="浏览", command=self.browse_file).grid(row=row, column=2, padx=5,
  109. pady=5)
  110. row += 1
  111. tk.Checkbutton(self.scrollable_frame, text="无沙盒模式", variable=self.params["NO_SANDBOX"]).grid(row=row,
  112. column=0,
  113. columnspan=3,
  114. pady=5)
  115. row += 1
  116. tk.Checkbutton(self.scrollable_frame, text="禁用 Dev SHM", variable=self.params["DISABLE_DEV_SHM"]).grid(
  117. row=row, column=0, columnspan=3, pady=5)
  118. row += 1
  119. tk.Checkbutton(self.scrollable_frame, text="启动时最大化", variable=self.params["START_MAXIMIZED"]).grid(
  120. row=row, column=0, columnspan=3, pady=5)
  121. row += 1
  122. self.status_label = tk.Label(self.scrollable_frame, text="状态: 就绪", fg="blue")
  123. self.status_label.grid(row=row, column=0, columnspan=3, pady=10)
  124. row += 1
  125. tk.Label(self.scrollable_frame, text="日志输出:").grid(row=row, column=0, columnspan=3, pady=5)
  126. row += 1
  127. # 日志窗口,初始高度为 0,动态调整
  128. self.log_frame = ttk.Frame(self.scrollable_frame)
  129. self.log_frame.grid(row=row, column=0, columnspan=3, padx=5, pady=5, sticky="nsew")
  130. self.log_text = tk.Text(self.log_frame, height=0, width=60, wrap=tk.WORD)
  131. self.log_scrollbar = ttk.Scrollbar(self.log_frame, orient=tk.VERTICAL, command=self.log_text.yview)
  132. self.log_text.configure(yscrollcommand=self.log_scrollbar.set)
  133. self.log_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
  134. self.log_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  135. row += 1
  136. # 按钮框架
  137. button_frame = ttk.Frame(self.scrollable_frame)
  138. button_frame.grid(row=row, column=0, columnspan=3, pady=10)
  139. tk.Button(button_frame, text="开始", command=self.start_script).pack(side=tk.LEFT, padx=5)
  140. tk.Button(button_frame, text="停止", command=self.stop_script).pack(side=tk.LEFT, padx=5)
  141. tk.Button(button_frame, text="清除日志", command=self.clear_log).pack(side=tk.LEFT, padx=5)
  142. tk.Button(button_frame, text="保存参数", command=self.save_config).pack(side=tk.LEFT, padx=5)
  143. row += 1
  144. # 设置列权重,使输入框宽度自适应
  145. for i in range(3):
  146. self.scrollable_frame.grid_columnconfigure(i, weight=1)
  147. # 设置日志处理器
  148. self.log_handler = TextHandler(self.log_text, self.adjust_log_height)
  149. logger.addHandler(self.log_handler)
  150. # 保存配置时绑定关闭事件
  151. self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
  152. # 调整窗口大小以适应内容
  153. self.root.update_idletasks()
  154. self.root.minsize(self.scrollable_frame.winfo_reqwidth(), self.scrollable_frame.winfo_reqheight())
  155. def adjust_log_height(self):
  156. """动态调整日志框高度"""
  157. content = self.log_text.get("1.0", tk.END).strip()
  158. if not content:
  159. self.log_text.configure(height=0)
  160. else:
  161. lines = content.count('\n') + 1
  162. max_height = 20
  163. new_height = min(lines, max_height)
  164. self.log_text.configure(height=new_height)
  165. def load_config(self):
  166. """加载配置文件"""
  167. self.config = {}
  168. if os.path.exists(self.config_file):
  169. try:
  170. with open(self.config_file, 'r', encoding='utf-8') as f:
  171. self.config = json.load(f)
  172. except Exception as e:
  173. logger.error(f"无法加载配置文件: {str(e)}")
  174. def save_config(self):
  175. """保存配置"""
  176. config = {
  177. "WINDOW_ID": self.params["WINDOW_ID"].get(),
  178. "BASE_URL": self.params["BASE_URL"].get(),
  179. "API_KEY": self.params["API_KEY"].get(),
  180. "FILE_PATH": self.params["FILE_PATH"].get(),
  181. "AMOUNT": self.params["AMOUNT"].get(),
  182. "SLEEP_TIME_1": self.params["SLEEP_TIME_1"].get(),
  183. "SLEEP_TIME_2": self.params["SLEEP_TIME_2"].get(),
  184. "SLEEP_TIME_3": self.params["SLEEP_TIME_3"].get(),
  185. "NO_SANDBOX": self.params["NO_SANDBOX"].get(),
  186. "DISABLE_DEV_SHM": self.params["DISABLE_DEV_SHM"].get(),
  187. "START_MAXIMIZED": self.params["START_MAXIMIZED"].get()
  188. }
  189. try:
  190. with open(self.config_file, 'w', encoding='utf-8') as f:
  191. json.dump(config, f, indent=4, ensure_ascii=False)
  192. messagebox.showinfo("提示", "参数已保存!")
  193. except Exception as e:
  194. logger.error(f"无法保存配置文件: {str(e)}")
  195. messagebox.showerror("错误", f"无法保存参数: {str(e)}")
  196. def on_closing(self):
  197. """关闭窗口时保存配置"""
  198. self.save_config()
  199. self.root.destroy()
  200. def browse_file(self):
  201. """文件选择器"""
  202. file_path = filedialog.askopenfilename(filetypes=[("文本文件", "*.txt"), ("所有文件", "*.*")])
  203. if file_path:
  204. self.params["FILE_PATH"].set(file_path)
  205. def clear_log(self):
  206. """清除日志"""
  207. self.log_text.delete(1.0, tk.END)
  208. self.adjust_log_height()
  209. def start_script(self):
  210. if self.running:
  211. messagebox.showinfo("提示", "脚本已经在运行!")
  212. return
  213. self.running = True
  214. self.status_label.config(text="状态: 运行中", fg="green")
  215. threading.Thread(target=self.run_script, daemon=True).start()
  216. def stop_script(self):
  217. self.running = False
  218. self.status_label.config(text="状态: 已停止", fg="red")
  219. # 立即关闭当前浏览器
  220. if self.current_web:
  221. try:
  222. self.current_web.quit()
  223. logger.info("因用户停止脚本,关闭 Selenium 驱动...")
  224. except Exception as e:
  225. logger.error(f"关闭 Selenium 驱动失败: {str(e)}")
  226. self.current_web = None
  227. def run_script(self):
  228. try:
  229. logger.info("开始运行脚本...")
  230. datas = read_txt_file(self.params["FILE_PATH"].get())
  231. if not datas:
  232. self.status_label.config(text="状态: 错误 - 文件未找到", fg="red")
  233. logger.error("文件未找到或为空")
  234. return
  235. logger.info(f"从文件中读取了 {len(datas)} 条记录")
  236. window_ids = self.params["WINDOW_ID"].get().split(',')
  237. logger.info(f"窗口ID列表: {window_ids}")
  238. from queue import Queue
  239. data_queue = Queue()
  240. for data in datas:
  241. data = [s.replace("#", "") for s in data]
  242. data_queue.put(data)
  243. # 为每个窗口ID启动一个线程
  244. threads = []
  245. for window_id in window_ids:
  246. thread = threading.Thread(target=self.process_window, args=(window_id, data_queue))
  247. thread.start()
  248. threads.append(thread)
  249. # 等待所有线程完成
  250. for thread in threads:
  251. thread.join()
  252. self.status_label.config(text="状态: 已完成", fg="blue")
  253. logger.info("脚本运行完成")
  254. except Exception as e:
  255. self.status_label.config(text=f"状态: 错误 - {str(e)}", fg="red")
  256. logger.error(f"运行脚本时出错: {str(e)}")
  257. finally:
  258. self.running = False
  259. def process_window(self, window_id, data_queue):
  260. while not data_queue.empty() and self.running:
  261. data = data_queue.get()
  262. success = self.process_data_for_window(data, window_id)
  263. if not success:
  264. data_queue.put(data) # 重新放回队列,等待其他线程处理
  265. data_queue.task_done()
  266. def process_data_for_window(self, data, window_id):
  267. """为每个窗口 ID 处理数据"""
  268. attempt_success = False
  269. max_retries = 3 # 最大重试次数
  270. for attempt in range(max_retries):
  271. try:
  272. # 启动浏览器并执行表单填写
  273. chrome_options = {
  274. "no_sandbox": self.params["NO_SANDBOX"].get(),
  275. "disable_dev_shm": self.params["DISABLE_DEV_SHM"].get(),
  276. "start_maximized": self.params["START_MAXIMIZED"].get()
  277. }
  278. sleep_times = {
  279. "sleep1": max(self.params["SLEEP_TIME_1"].get(), 60.0), # 强制至少 60 秒
  280. "sleep2": self.params["SLEEP_TIME_2"].get(),
  281. "sleep3": self.params["SLEEP_TIME_3"].get()
  282. }
  283. logger.info(f"使用的等待时间: sleep1={sleep_times['sleep1']}, sleep2={sleep_times['sleep2']}, sleep3={sleep_times['sleep3']}")
  284. result = start_Ads_data(self, data, window_id, sleep_times, chrome_options, self.params["AMOUNT"].get(),
  285. self.params["API_KEY"].get())
  286. if result:
  287. logger.info("操作成功!")
  288. # 删除数据
  289. with open(self.params["FILE_PATH"].get(), "r") as f:
  290. lines = f.readlines()
  291. # 保留不匹配的行
  292. filtered_lines = [line for line in lines if data[0] not in line]
  293. with open(self.params["FILE_PATH"].get(), 'w') as f:
  294. f.writelines(filtered_lines)
  295. attempt_success = True
  296. break # 成功则跳出循环
  297. else:
  298. logger.warning(f"查询超时已记录")
  299. attempt_success = True
  300. break # 返回 False 也跳出循环,并记录
  301. except Exception as e:
  302. if not self.running:
  303. logger.info(f"{window_id} 用户停止了脚本")
  304. break
  305. if attempt < max_retries - 1:
  306. logger.warning(f"{window_id} 尝试 {attempt + 1} 失败: {str(e)},等待 10 秒后重试...")
  307. # 确保关闭浏览器窗口
  308. if self.current_web:
  309. try:
  310. self.current_web.quit()
  311. logger.info(f"关闭 Selenium 驱动以准备重试...")
  312. except Exception as close_err:
  313. logger.error(f"关闭 Selenium 驱动失败: {str(close_err)}")
  314. self.current_web = None
  315. closeBrowser(window_id)
  316. time.sleep(sleep_times["sleep3"])
  317. else:
  318. logger.error(f"处理 {window_id} 记录 {data[0]} 时出错: {str(e)},达到最大重试次数,失败!")
  319. break
  320. if not attempt_success:
  321. logger1.info(f"{data}{window_id} 注册失败,放入其他线程再次填写")
  322. logger.info("完成一条记录,进入下一条记录")
  323. return attempt_success
  324. def start_Ads_data(gui, data, window_id, sleep_times, chrome_options, amount, API_KEY):
  325. # 处理表单填写,严格按照原始代码的流程:先填写表单,后检测验证码
  326. web = start_ADS(window_id, chrome_options, gui, sleep_times, API_KEY)
  327. # 滑动到页面的底部
  328. web.execute_script("window.scrollTo(0, document.body.scrollHeight);")
  329. # 等待验证码插件初始化
  330. logger.info("等待验证码插件初始化...")
  331. time.sleep(20) # 初始等待 20 秒,确保插件加载
  332. # 开始填写表单
  333. logger.info("开始填写表单...")
  334. web.find_element(By.XPATH, "//input[@type='text']").send_keys(data[1]) # 姓名
  335. logger.info("填写姓名完成")
  336. web.find_element(By.ID, "street1").send_keys(data[2]) # 街道地址
  337. logger.info("填写街道地址完成")
  338. web.find_element(By.ID, "city").send_keys(data[3]) # 城市
  339. logger.info("填写城市完成")
  340. time.sleep(1)
  341. Select(web.find_element(By.XPATH,
  342. "(.//*[normalize-space(text()) and normalize-space(.)='*'])[5]/preceding::select[1]")).select_by_index(
  343. abbreviation_to_index[data[4]]) # 州
  344. logger.info("选择州完成")
  345. web.find_element(By.ID, "zip").send_keys(data[5]) # 邮编
  346. logger.info("填写邮编完成")
  347. time.sleep(1)
  348. web.find_element(By.ID, "email").send_keys(data[0]) # 邮箱
  349. logger.info("填写邮箱完成")
  350. # 使用 JavaScript 点击复选框
  351. logger.info("点击复选框...")
  352. try:
  353. checkbox = WebDriverWait(web, 10).until(
  354. EC.presence_of_element_located((By.XPATH, "//input[@type='checkbox']"))
  355. )
  356. web.execute_script("arguments[0].scrollIntoView(true);", checkbox)
  357. web.execute_script("arguments[0].click();", checkbox)
  358. logger.info("通过 JavaScript 点击复选框完成")
  359. except Exception as e:
  360. logger.warning(f"点击复选框失败: {str(e)},尝试使用更复杂的 JavaScript 点击...")
  361. web.execute_script("""
  362. var checkbox = document.querySelector('input[type="checkbox"]');
  363. if (checkbox) {
  364. var event = new Event('click', { bubbles: true });
  365. checkbox.dispatchEvent(event);
  366. }
  367. """)
  368. logger.info("通过复杂 JavaScript 点击复选框完成")
  369. # 使用 JavaScript 选择金额范围
  370. logger.info("选择金额范围...")
  371. try:
  372. amount_radio = WebDriverWait(web, 10).until(
  373. EC.presence_of_element_located((By.XPATH, f"//input[@value='{amount}']"))
  374. )
  375. web.execute_script("arguments[0].scrollIntoView(true);", amount_radio)
  376. web.execute_script("arguments[0].click();", amount_radio)
  377. logger.info("通过 JavaScript 选择金额范围完成")
  378. except Exception as e:
  379. logger.warning(f"选择金额范围失败: {str(e)},尝试使用更复杂的 JavaScript 点击...")
  380. web.execute_script(f"""
  381. var radio = document.querySelector('input[value="{amount}"]');
  382. if (radio) {{
  383. var event = new Event('click', {{ bubbles: true }});
  384. radio.dispatchEvent(event);
  385. }}
  386. """)
  387. logger.info("通过复杂 JavaScript 选择金额范围完成")
  388. web.find_element(By.ID, "signature").send_keys(data[1]) # 签名
  389. logger.info("填写签名完成")
  390. logger.info("表单填写完成,开始检测验证码...")
  391. # 注入全局监听器
  392. logger.info("注入全局监听器以检测验证码...")
  393. web.execute_script("""
  394. window.recaptchaVerified = false;
  395. function checkRecaptcha() {
  396. var recaptchaResponse = document.getElementById('g-recaptcha-response');
  397. if (recaptchaResponse && recaptchaResponse.value && recaptchaResponse.value.trim() !== '') {
  398. window.recaptchaVerified = true;
  399. console.log('reCAPTCHA 验证成功: ' + recaptchaResponse.value);
  400. }
  401. }
  402. // 每 500 毫秒检查一次
  403. window.recaptchaInterval = setInterval(checkRecaptcha, 500);
  404. """)
  405. # 检测验证码(动态检测)
  406. logger.info("开始检测验证码...")
  407. max_attempts = 3 # 最大尝试次数
  408. attempts = 0
  409. total_wait_time = sleep_times["sleep1"] # 总等待时间(60 秒)
  410. poll_interval = 1 # 轮询间隔(每 1 秒检查一次)
  411. elapsed_time = 0
  412. while attempts < max_attempts and elapsed_time < total_wait_time:
  413. # 检查 g-recaptcha-response 元素状态
  414. try:
  415. element = web.find_element(By.ID, 'g-recaptcha-response')
  416. response_value = element.get_attribute('value')
  417. logger.info(f"g-recaptcha-response 元素状态: display={element.value_of_css_property('display')}, value={response_value}")
  418. if response_value and response_value.strip() != '':
  419. logger.info("reCAPTCHA 验证成功(通过 Selenium 检测)")
  420. break
  421. except Exception as e:
  422. logger.debug(f"无法找到 g-recaptcha-response 元素: {str(e)}")
  423. # 检查全局变量
  424. verified = web.execute_script("return window.recaptchaVerified;")
  425. if verified:
  426. logger.info("reCAPTCHA 验证成功(通过全局监听器)")
  427. break
  428. # 检查 reCAPTCHA 勾选框状态(可选)
  429. try:
  430. checkbox = WebDriverWait(web, 2).until(
  431. EC.presence_of_element_located((By.CSS_SELECTOR, "span.recaptcha-checkbox-checked"))
  432. )
  433. logger.info("reCAPTCHA 勾选框已变成绿色,额外等待 10 秒...")
  434. time.sleep(10) # 额外等待 10 秒
  435. elapsed_time += 10
  436. logger.info(f"已等待 {elapsed_time} 秒,剩余 {total_wait_time - elapsed_time} 秒...")
  437. continue
  438. except TimeoutException:
  439. logger.debug("reCAPTCHA 勾选框未变成绿色,继续等待...")
  440. # 等待一段时间后重试
  441. elapsed_time += poll_interval
  442. logger.info(f"已等待 {elapsed_time} 秒,剩余 {total_wait_time - elapsed_time} 秒...")
  443. time.sleep(poll_interval)
  444. if elapsed_time >= total_wait_time:
  445. attempts += 1
  446. logger.warning(f"第 {attempts} 次尝试:验证码验证未完成或失败")
  447. if attempts >= max_attempts:
  448. logger.error("验证码识别失败,达到最大尝试次数,跳过当前记录")
  449. # 清理监听器
  450. web.execute_script("if (window.recaptchaInterval) clearInterval(window.recaptchaInterval);")
  451. if web:
  452. try:
  453. web.quit()
  454. logger.info("关闭 Selenium 驱动...")
  455. except Exception as close_err:
  456. logger.error(f"关闭 Selenium 驱动失败: {str(close_err)}")
  457. gui.current_web = None
  458. closeBrowser(window_id)
  459. time.sleep(sleep_times["sleep3"])
  460. return False
  461. else:
  462. logger.info("刷新页面并重试...")
  463. # 清理监听器
  464. web.execute_script("if (window.recaptchaInterval) clearInterval(window.recaptchaInterval);")
  465. web.refresh()
  466. time.sleep(5) # 等待页面刷新
  467. # 重新注入监听器
  468. web.execute_script("""
  469. window.recaptchaVerified = false;
  470. function checkRecaptcha() {
  471. var recaptchaResponse = document.getElementById('g-recaptcha-response');
  472. if (recaptchaResponse && recaptchaResponse.value && recaptchaResponse.value.trim() !== '') {
  473. window.recaptchaVerified = true;
  474. console.log('reCAPTCHA 验证成功: ' + recaptchaResponse.value);
  475. }
  476. }
  477. window.recaptchaInterval = setInterval(checkRecaptcha, 500);
  478. """)
  479. # 重新填写表单
  480. logger.info("重新填写表单...")
  481. web.find_element(By.XPATH, "//input[@type='text']").send_keys(data[1]) # 姓名
  482. logger.info("重新填写姓名完成")
  483. web.find_element(By.ID, "street1").send_keys(data[2]) # 街道地址
  484. logger.info("重新填写街道地址完成")
  485. web.find_element(By.ID, "city").send_keys(data[3]) # 城市
  486. logger.info("重新填写城市完成")
  487. time.sleep(1)
  488. Select(web.find_element(By.XPATH,
  489. "(.//*[normalize-space(text()) and normalize-space(.)='*'])[5]/preceding::select[1]")).select_by_index(
  490. abbreviation_to_index[data[4]]) # 州
  491. logger.info("重新选择州完成")
  492. web.find_element(By.ID, "zip").send_keys(data[5]) # 邮编
  493. logger.info("重新填写邮编完成")
  494. time.sleep(1)
  495. web.find_element(By.ID, "email").send_keys(data[0]) # 邮箱
  496. logger.info("重新填写邮箱完成")
  497. # 重新点击复选框
  498. logger.info("重新点击复选框...")
  499. try:
  500. checkbox = WebDriverWait(web, 10).until(
  501. EC.presence_of_element_located((By.XPATH, "//input[@type='checkbox']"))
  502. )
  503. web.execute_script("arguments[0].scrollIntoView(true);", checkbox)
  504. web.execute_script("arguments[0].click();", checkbox)
  505. logger.info("通过 JavaScript 重新点击复选框完成")
  506. except Exception as e:
  507. logger.warning(f"重新点击复选框失败: {str(e)},尝试使用更复杂的 JavaScript 点击...")
  508. web.execute_script("""
  509. var checkbox = document.querySelector('input[type="checkbox"]');
  510. if (checkbox) {
  511. var event = new Event('click', { bubbles: true });
  512. checkbox.dispatchEvent(event);
  513. }
  514. """)
  515. logger.info("通过复杂 JavaScript 重新点击复选框完成")
  516. # 重新选择金额范围
  517. logger.info("重新选择金额范围...")
  518. try:
  519. amount_radio = WebDriverWait(web, 10).until(
  520. EC.presence_of_element_located((By.XPATH, f"//input[@value='{amount}']"))
  521. )
  522. web.execute_script("arguments[0].scrollIntoView(true);", amount_radio)
  523. web.execute_script("arguments[0].click();", amount_radio)
  524. logger.info("通过 JavaScript 重新选择金额范围完成")
  525. except Exception as e:
  526. logger.warning(f"重新选择金额范围失败: {str(e)},尝试使用更复杂的 JavaScript 点击...")
  527. web.execute_script(f"""
  528. var radio = document.querySelector('input[value="{amount}"]');
  529. if (radio) {{
  530. var event = new Event('click', {{ bubbles: true }});
  531. radio.dispatchEvent(event);
  532. }}
  533. """)
  534. logger.info("通过复杂 JavaScript 重新选择金额范围完成")
  535. web.find_element(By.ID, "signature").send_keys(data[1]) # 签名
  536. logger.info("重新填写签名完成")
  537. elapsed_time = 0 # 重置等待时间
  538. # 清理监听器
  539. web.execute_script("if (window.recaptchaInterval) clearInterval(window.recaptchaInterval);")
  540. # 使用 JavaScript 提交表单
  541. logger.info("提交表单...")
  542. try:
  543. submit_button = WebDriverWait(web, 10).until(
  544. EC.presence_of_element_located((By.XPATH, "//button[@type='submit']"))
  545. )
  546. web.execute_script("arguments[0].scrollIntoView(true);", submit_button)
  547. web.execute_script("arguments[0].click();", submit_button)
  548. logger.info("通过 JavaScript 提交表单完成")
  549. except Exception as e:
  550. logger.warning(f"提交表单失败: {str(e)},尝试使用更复杂的 JavaScript 点击...")
  551. web.execute_script("""
  552. var button = document.querySelector('button[type="submit"]');
  553. if (button) {
  554. var event = new Event('click', { bubbles: true });
  555. button.dispatchEvent(event);
  556. }
  557. """)
  558. logger.info("通过复杂 JavaScript 提交表单完成")
  559. # 验证提取是否成功
  560. result = check_submission_success(data, web)
  561. # 清理浏览器
  562. time.sleep(sleep_times["sleep2"])
  563. web.delete_all_cookies()
  564. web.execute_script("window.localStorage.clear();")
  565. web.execute_script("window.sessionStorage.clear();")
  566. if web:
  567. try:
  568. web.quit()
  569. logger.info("关闭 Selenium 驱动...")
  570. except Exception as close_err:
  571. logger.error(f"关闭 Selenium 驱动失败: {str(close_err)}")
  572. gui.current_web = None
  573. closeBrowser(window_id)
  574. # 添加延迟,确保窗口完全关闭
  575. logger.info("等待窗口完全关闭...")
  576. time.sleep(sleep_times["sleep3"])
  577. logger.info("完成一次填表循环")
  578. return result
  579. def start_ADS(window_id, chrome_options, gui, sleep_times, API_KEY):
  580. # 开启窗口,打开页面
  581. max_attempts = 3 # 最大尝试次数
  582. for attempt in range(max_attempts):
  583. try:
  584. res = openBrowser(window_id)
  585. # 调试:打印 openBrowser 的返回值
  586. logger.info(f"openBrowser Response: {res}")
  587. # 检查返回值是否包含 'data' 键
  588. if 'data' not in res:
  589. logger.error("BitBrowser API 返回中没有 'data' 键")
  590. raise Exception("BitBrowser API 返回中没有 'data' 键")
  591. driverPath = res['data']['driver']
  592. debuggerAddress = res['data']['http']
  593. if res["success"] is True:
  594. logger.info("启动成功,返回信息")
  595. else:
  596. logger.error(f"{window_id} 启动失败")
  597. raise Exception("启动失败")
  598. chrome_opts = webdriver.ChromeOptions()
  599. chrome_opts.add_experimental_option("debuggerAddress", debuggerAddress)
  600. if chrome_options["no_sandbox"]:
  601. chrome_opts.add_argument('--no-sandbox')
  602. if chrome_options["disable_dev_shm"]:
  603. chrome_opts.add_argument('--disable-dev-shm-usage')
  604. if chrome_options["start_maximized"]:
  605. chrome_opts.add_argument('--start-maximized')
  606. chrome_service = Service(driverPath)
  607. web = webdriver.Chrome(service=chrome_service, options=chrome_opts)
  608. web.implicitly_wait(0)
  609. # 将 web 实例保存到 GUI 对象,以便在停止时关闭
  610. gui.current_web = web
  611. logger.info("加载页面...")
  612. web.get("https://claimform.savingsclubsettlement.com/consumerb-claimants")
  613. original_window = web.current_window_handle
  614. WebDriverWait(web, 10).until(EC.presence_of_element_located((By.TAG_NAME, 'body')))
  615. logger.info(f"当前页面标题: {web.title}")
  616. return web
  617. except Exception as e:
  618. logger.info(f"加载页面失败: {str(e)},将重启窗口 {window_id}")
  619. # 关闭窗口
  620. if gui.current_web:
  621. try:
  622. gui.current_web.quit()
  623. logger.info("关闭 Selenium 驱动以准备重试...")
  624. except Exception as close_err:
  625. logger.error(f"关闭 Selenium 驱动失败: {str(close_err)}")
  626. gui.current_web = None
  627. closeBrowser(window_id)
  628. time.sleep(sleep_times["sleep3"])
  629. if attempt == max_attempts - 1:
  630. logger.error(f"启动窗口 {window_id} 失败,达到最大尝试次数")
  631. raise Exception("无法启动浏览器")
  632. return web
  633. def check_submission_success(data, web):
  634. # 验证是否提交成功并获取参考号
  635. try:
  636. # 等待成功标题出现
  637. WebDriverWait(web, 30).until(
  638. EC.visibility_of_element_located((By.XPATH, "//h2[contains(., 'THANK YOU FOR SUBMITTING')]"))
  639. )
  640. # 获取参考号和日期
  641. ref_number = web.find_element(By.XPATH, "//p[contains(., 'Your Reference Number is')]/b").text
  642. # submission_date = web.find_element(By.XPATH, "//p[contains(., 'submitted on')]/b").text
  643. logger1.info(", ".join(data) + "提交成功参考号为:" + ref_number)
  644. return True
  645. except Exception as e:
  646. logger.warning({str(e)})
  647. logger.warning(", ".join(data) + "验证失败")
  648. logger1.info(", ".join(data) + "提交后查询参考号失败")
  649. # 这里不抛异常
  650. return False
  651. def read_txt_file(file_path):
  652. data = []
  653. try:
  654. with open(file_path, 'r', encoding='utf-8') as file:
  655. for line in file:
  656. cleaned_line = line.strip()
  657. if not cleaned_line:
  658. continue
  659. fields = cleaned_line.split('\t')
  660. if len(fields) != 6:
  661. logger.warning(f"第 {len(data) + 1} 行字段数量异常: {len(fields)}")
  662. continue
  663. data.append(fields)
  664. except FileNotFoundError:
  665. logger.error(f"文件 {file_path} 未找到")
  666. return None
  667. except Exception as e:
  668. logger.error(f"读取文件时发生错误: {str(e)}")
  669. return None
  670. return data
  671. if __name__ == '__main__':
  672. root = tk.Tk()
  673. app = AdsDataGUI(root)
  674. root.mainloop()
  675. # 填表,监测验证码,
  676. # ,5c2216ed3b3e4bf0bc144b5ad0c87a5b