Improve KDocs search matching
This commit is contained in:
@@ -5,6 +5,7 @@ from __future__ import annotations
|
||||
import base64
|
||||
import os
|
||||
import queue
|
||||
import re
|
||||
import threading
|
||||
import time
|
||||
from io import BytesIO
|
||||
@@ -822,17 +823,57 @@ class KDocsUploader:
|
||||
pass
|
||||
return ""
|
||||
|
||||
def _normalize_text(self, value: str) -> str:
|
||||
if value is None:
|
||||
return ""
|
||||
cleaned = str(value)
|
||||
cleaned = cleaned.replace("\u00a0", "").replace("\u3000", "")
|
||||
cleaned = re.sub(r"\s+", "", cleaned)
|
||||
return cleaned.strip()
|
||||
|
||||
def _unit_matches(self, cell_value: str, expected_unit: str) -> bool:
|
||||
if not cell_value or not expected_unit:
|
||||
return False
|
||||
norm_cell = self._normalize_text(cell_value)
|
||||
norm_expected = self._normalize_text(expected_unit)
|
||||
if not norm_cell or not norm_expected:
|
||||
return False
|
||||
if norm_cell == norm_expected:
|
||||
return True
|
||||
if norm_expected in norm_cell or norm_cell in norm_expected:
|
||||
return True
|
||||
return False
|
||||
|
||||
def _search_person(self, name: str) -> None:
|
||||
self._page.keyboard.press("Control+f")
|
||||
time.sleep(0.3)
|
||||
search_input = None
|
||||
selectors = [
|
||||
"input[placeholder*='查找']",
|
||||
"input[placeholder*='搜索']",
|
||||
"input[aria-label*='查找']",
|
||||
"input[aria-label*='搜索']",
|
||||
"input[type='search']",
|
||||
]
|
||||
try:
|
||||
search_input = self._page.get_by_role("textbox").nth(3)
|
||||
if search_input.is_visible(timeout=800):
|
||||
search_input.fill(name)
|
||||
except Exception:
|
||||
search_input = None
|
||||
if not search_input:
|
||||
for selector in selectors:
|
||||
try:
|
||||
search_input = self._page.locator("input[placeholder*='查找']").first
|
||||
candidate = self._page.locator(selector).first
|
||||
if candidate.is_visible(timeout=800):
|
||||
search_input = candidate
|
||||
search_input.fill(name)
|
||||
break
|
||||
except Exception:
|
||||
continue
|
||||
if not search_input:
|
||||
try:
|
||||
self._page.keyboard.type(name)
|
||||
except Exception:
|
||||
pass
|
||||
time.sleep(0.2)
|
||||
@@ -842,6 +883,9 @@ class KDocsUploader:
|
||||
except Exception:
|
||||
try:
|
||||
self._page.get_by_role("button", name="查找").first.click()
|
||||
except Exception:
|
||||
try:
|
||||
self._page.keyboard.press("Enter")
|
||||
except Exception:
|
||||
pass
|
||||
time.sleep(0.3)
|
||||
@@ -853,6 +897,9 @@ class KDocsUploader:
|
||||
except Exception:
|
||||
try:
|
||||
self._page.get_by_role("button", name="查找").first.click()
|
||||
except Exception:
|
||||
try:
|
||||
self._page.keyboard.press("Enter")
|
||||
except Exception:
|
||||
pass
|
||||
time.sleep(0.3)
|
||||
@@ -873,7 +920,7 @@ class KDocsUploader:
|
||||
cell_address = f"{unit_col}{row_num}"
|
||||
cell_value = self._get_cell_value(cell_address)
|
||||
if cell_value:
|
||||
return cell_value == unit
|
||||
return self._unit_matches(cell_value, unit)
|
||||
return False
|
||||
|
||||
def _find_person_with_unit(self, unit: str, name: str, unit_col: str, max_attempts: int = 50) -> int:
|
||||
|
||||
Reference in New Issue
Block a user