移动端登录问题修复

This commit is contained in:
SwZ
2025-05-28 11:06:57 +08:00
parent 137f2d5562
commit 218812b4bc
2 changed files with 41 additions and 51 deletions

View File

@@ -1364,67 +1364,56 @@ def playwright_m_login(username, password, code_2fa=None):
page = context.new_page()
url = 'https://m.facebook.com/login'
retry_goto(page, url)
page.wait_for_load_state('networkidle')
# 概率会跳到首页
have_account = 'div[role="button"][aria-label="I already have an account"]'
# 用户名输入框框
input_email = "input#m_login_email"
try:
page.wait_for_selector(f'{have_account}, {input_email}', timeout=60000)
except Exception as e:
logger.error(f"页面加载异常, 未定位到按钮", exc_info=True)
raise OperationFailed("系统异常")
hava_account_btn = page.query_selector(have_account)
if hava_account_btn:
hava_account_btn.click()
page.wait_for_selector(f'{input_email}', timeout=60000).type(username, delay=30)
page.wait_for_selector('//input[@id="m_login_email"]', timeout=60000).type(username, delay=30)
time.sleep(1)
page.wait_for_selector(f'//input[@id="m_login_password"]', timeout=60000).type(password, delay=30)
time.sleep(1)
page.click('div[aria-label="Log in"]')
page.wait_for_selector('//div[@aria-label="Loading..."]', state='detached')
success_login_selector1 = 'span:has-text("Check your notifications on another device")'
success_login_selector2 = 'span:has-text("Go to your authentication app")'
failed_login_selector = 'div[data-bloks-name="bk.components.dialog.Dialog"] > div[aria-label="Wrong Credentials"] > div:nth-child(1)'
# 等成功或失败两个选择的其中一个
page.wait_for_selector(f'{success_login_selector1},{success_login_selector2}, {failed_login_selector}',
timeout=60000)
time.sleep(5)
login_wrong = page.query_selector('//div[@aria-label="Wrong Credentials"]')
if login_wrong:
# 账号密码错误报错
wrong_text = page.locator('//div[@aria-label="Wrong Credentials"]/div/div[2]').first.inner_text()
raise AuthException(wrong_text)
# 判断是否失败
failed_login = page.query_selector(failed_login_selector)
if failed_login:
# 登录失败, 返回失败信息
logger.info(f"用户名:{username} 密码错误 {failed_login.text_content()}")
raise OperationFailed(failed_login.text_content())
# 身份验证
auth_span = page.query_selector('//span[text()="Try Another Way" or text()="Try another way"]')
if auth_span:
if not code_2fa:
raise OperationFailed('缺少2FA密钥')
auth_code = pyotp.TOTP(code_2fa).now()
auth_span.click()
page.wait_for_selector('//span[text()="Authentication app"]').click()
page.locator('//span[text()="Continue"]').first.click()
page.wait_for_selector('//input[@aria-label="Code"]').fill(auth_code)
page.locator('//span[text()="Continue"]').first.click()
page.wait_for_selector('//div[@data-visualcompletion="loading-state"]', state='detached')
time.sleep(5)
auth_wrong = page.query_selector('//span[text()="This code doesnt work. Check its correct or try a new one."]')
if auth_wrong:
raise AuthException('f2a代码错误')
# 判断是否有多重验证
success_login1 = page.query_selector(success_login_selector1)
if success_login1:
# 点击尝试另一种方式验证账号
page.query_selector('div[role="button"][aria-label="Try another way"]').click()
# 点击选择app验证
page.wait_for_selector(
'span[data-bloks-name="bk.components.TextSpan"]:has-text("Authentication app")').click()
# 点击继续
page.query_selector('div[role="button"][aria-label="Continue"]').click()
# 等待页面
page.wait_for_selector('span:has-text("Go to your authentication app")', timeout=60000)
# 输入2faCode
auth_code = pyotp.TOTP(code_2fa).now()
page.wait_for_selector('input[aria-label="Code"][type="text"]', timeout=60000).fill(auth_code)
# 点击继续
page.query_selector('div[role="button"][aria-label="Continue"]').click()
# 等待登录成功
page.wait_for_selector('img[data-bloks-name="bk.components.Image"][src*=".fbcdn.net/v/t"]',timeout=60000)
# 保存登录
save_span = page.query_selector('//span[text()="Save"]')
if save_span:
save_span.click()
page.wait_for_selector('//div[@data-visualcompletion="loading-state"]', state='detached')
# 等待页面加载
page.wait_for_load_state()
try:
page.wait_for_selector('//div[@role="tab"]', timeout=60000)
except TimeoutError:
pass
# 成功
logger.info(f"用户名:{username} 账号密码正确")