在tab访问指定URL后,增加了等待URL变化并自动处理登录流程的逻辑。如果URL以"/auth/login"结尾,自动输入用户名和密码并点击登录按钮,以确保后续操作在登录状态下进行。
47 lines
1.1 KiB
Python
47 lines
1.1 KiB
Python
from DrissionPage import Chromium, ChromiumOptions, SessionPage
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
import atexit
|
|
|
|
import listener.console
|
|
import listener.network
|
|
|
|
|
|
options = ChromiumOptions()
|
|
# 无头模式
|
|
# options.headless()
|
|
|
|
import os
|
|
os.environ.setdefault('PYTHONIOENCODING', 'utf-8')
|
|
print(os.environ)
|
|
|
|
browser = Chromium(options)
|
|
tab = browser.latest_tab
|
|
|
|
executor = ThreadPoolExecutor(max_workers=2)
|
|
executor.submit(listener.console.console_listener, tab)
|
|
executor.submit(listener.network.network_listener, tab)
|
|
|
|
|
|
tab.get(url='http://127.0.0.1:24613')
|
|
|
|
tab.wait.url_change(text="/auth/login", timeout=3, raise_err=False)
|
|
|
|
if tab.url.endswith("/auth/login"):
|
|
tab.wait.doc_loaded()
|
|
tab.actions.move_to("@@t()=input@@type=text").click().type("shikong")
|
|
tab.actions.move_to("@@t()=input@@type=password").click().type("123+AbC")
|
|
tab.actions.move_to("@@t()=button@@text():登录").click()
|
|
|
|
tab.change_mode()
|
|
|
|
items = tab.eles("t:body")
|
|
for item in items:
|
|
print(item)
|
|
|
|
def exit():
|
|
print('exit')
|
|
browser.quit()
|
|
executor.shutdown()
|
|
|
|
atexit.register(exit)
|