Trigger KDocs WeChat login flow

This commit is contained in:
2026-01-07 13:26:31 +08:00
parent d8897f893a
commit b78bc7935f

View File

@@ -210,6 +210,12 @@ class KDocsUploader:
return False
except Exception:
return False
try:
login_btn = self._page.get_by_role("button", name="立即登录")
if login_btn.is_visible(timeout=1200):
return False
except Exception:
pass
try:
login_btn = self._page.get_by_role("button", name="登录")
if login_btn.is_visible(timeout=1200):
@@ -235,37 +241,27 @@ class KDocsUploader:
return os.path.exists(storage_state)
def _ensure_login_dialog(self) -> None:
try:
login_btn = self._page.get_by_role("button", name="登录并加入编辑")
if login_btn.is_visible(timeout=1500):
login_btn.click()
time.sleep(1)
except Exception:
pass
try:
login_btn = self._page.get_by_role("button", name="登录")
if login_btn.is_visible(timeout=1500):
login_btn.click()
time.sleep(1)
except Exception:
pass
try:
login_link = self._page.get_by_role("link", name="登录")
if login_link.is_visible(timeout=1500):
login_link.click()
time.sleep(1)
except Exception:
pass
try:
wechat_btn = self._page.get_by_role("button", name="微信登录")
if wechat_btn.is_visible(timeout=3000):
wechat_btn.click()
time.sleep(1.5)
except Exception:
pass
login_names = ["登录并加入编辑", "立即登录", "登录"]
wechat_names = ["微信登录", "微信扫码登录", "微信扫码", "扫码登录"]
pages = self._iter_pages()
clicked = False
for page in pages:
for name in login_names:
if self._try_click_role(page, "button", name):
clicked = True
break
if clicked:
continue
for name in login_names:
if self._try_click_role(page, "link", name):
clicked = True
break
if clicked:
time.sleep(1.5)
pages = self._iter_pages()
for page in pages:
if self._try_click_names(page, wechat_names):
return
def _capture_qr_image(self) -> Optional[bytes]:
pages = self._iter_pages()
@@ -382,6 +378,46 @@ class KDocsUploader:
pass
return best
def _try_click_role(self, page, role: str, name: str, timeout: int = 1500) -> bool:
try:
el = page.get_by_role(role, name=name)
if el.is_visible(timeout=timeout):
el.click()
time.sleep(1)
return True
except Exception:
return False
return False
def _try_click_names(self, page, names: list) -> bool:
for name in names:
if self._try_click_role(page, "button", name, timeout=1200):
return True
if self._try_click_role(page, "link", name, timeout=1200):
return True
try:
for frame in page.frames:
for name in names:
try:
el = frame.get_by_role("button", name=name)
if el.is_visible(timeout=800):
el.click()
time.sleep(1)
return True
except Exception:
pass
try:
el = frame.get_by_text(name, exact=True)
if el.is_visible(timeout=800):
el.click()
time.sleep(1)
return True
except Exception:
pass
except Exception:
return False
return False
def _capture_dialog_image(self, page) -> Optional[bytes]:
selectors = "[role='dialog'], .dialog, .modal, .popup"
try:
@@ -442,6 +478,7 @@ class KDocsUploader:
self._ensure_login_dialog()
qr_image = None
for _ in range(10):
self._ensure_login_dialog()
qr_image = self._capture_qr_image()
if qr_image:
break