Verbessere CAPTCHA-Handling im Login-Prozess: Implementiere automatische Token-Generierung und verbessere Fehlerbehandlung.
This commit is contained in:
@@ -37,17 +37,44 @@ async function loginWithBrowser(email, password) {
|
|||||||
const browser = await chromium.launch({ headless: true })
|
const browser = await chromium.launch({ headless: true })
|
||||||
try {
|
try {
|
||||||
const page = await browser.newPage()
|
const page = await browser.newPage()
|
||||||
await page.goto(`${BASE_URL}/login?next=%2F`, { waitUntil: 'networkidle' })
|
// Das myTT-CAPTCHA wird erst nach dem Laden der Seite erzeugt. Der Ablauf
|
||||||
|
// entspricht der erprobten Integration im Trainingstagebuch.
|
||||||
|
await page.goto(BASE_URL, { waitUntil: 'domcontentloaded', timeout: 30_000 }).catch(() => {})
|
||||||
|
await page.goto(`${BASE_URL}/login?next=%2F`, { waitUntil: 'domcontentloaded', timeout: 45_000 })
|
||||||
await page.locator('input[name="email"]').fill(email)
|
await page.locator('input[name="email"]').fill(email)
|
||||||
await page.locator('input[name="password"]').fill(password)
|
await page.locator('input[name="password"]').fill(password)
|
||||||
// Das CAPTCHA wird vom Anbieter im Browser erzeugt. Ohne erfolgreiches Token wird nicht abgesendet.
|
|
||||||
await page.waitForFunction(() => {
|
const captchaHost = page.locator('private-captcha').first()
|
||||||
const input = document.querySelector('input[name="captcha"]')
|
const hasCaptcha = await captchaHost.count() > 0
|
||||||
return Boolean(input?.value)
|
if (hasCaptcha) {
|
||||||
}, { timeout: 25_000 })
|
await page.waitForTimeout(1_200)
|
||||||
await page.locator('form').evaluate(form => form.requestSubmit())
|
await page.evaluate(() => {
|
||||||
await page.waitForTimeout(1_500)
|
const host = document.querySelector('private-captcha')
|
||||||
const cookie = (await page.context().cookies(BASE_URL)).find(item => item.name === 'sb-10-auth-token')
|
const checkbox = host?.shadowRoot?.querySelector('#pc-checkbox')
|
||||||
|
if (!checkbox) return
|
||||||
|
checkbox.click()
|
||||||
|
checkbox.dispatchEvent(new Event('input', { bubbles: true }))
|
||||||
|
checkbox.dispatchEvent(new Event('change', { bubbles: true }))
|
||||||
|
})
|
||||||
|
await page.waitForFunction(() => {
|
||||||
|
const token = document.querySelector('input[name="captcha"]')?.value?.trim() || ''
|
||||||
|
const clicked = document.querySelector('input[name="captcha_clicked"]')?.value?.toLowerCase() || ''
|
||||||
|
return token.length > 80 && (clicked === 'true' || clicked === '1')
|
||||||
|
}, { timeout: 32_000 }).catch(() => {})
|
||||||
|
const captchaReady = await page.evaluate(() => (document.querySelector('input[name="captcha"]')?.value?.trim().length || 0) > 80)
|
||||||
|
if (!captchaReady) throw new Error('CAPTCHA konnte nicht automatisch gelöst werden.')
|
||||||
|
await page.waitForTimeout(2_500)
|
||||||
|
}
|
||||||
|
|
||||||
|
const submit = page.locator('button[type="submit"][name="intent"][value="login"], button[type="submit"], input[type="submit"]').first()
|
||||||
|
if (await submit.count()) await submit.click({ noWaitAfter: true })
|
||||||
|
else await page.locator('form').evaluate(form => form.requestSubmit())
|
||||||
|
let cookie = null
|
||||||
|
for (let attempt = 0; attempt < 40; attempt += 1) {
|
||||||
|
cookie = (await page.context().cookies(BASE_URL)).find(item => /^sb-\d+-auth-token$/.test(item.name))
|
||||||
|
if (cookie) break
|
||||||
|
await page.waitForTimeout(500)
|
||||||
|
}
|
||||||
if (!cookie) throw new Error('CAPTCHA konnte nicht automatisch gelöst werden.')
|
if (!cookie) throw new Error('CAPTCHA konnte nicht automatisch gelöst werden.')
|
||||||
return `${cookie.name}=${cookie.value}`
|
return `${cookie.name}=${cookie.value}`
|
||||||
} finally { await browser.close() }
|
} finally { await browser.close() }
|
||||||
|
|||||||
Reference in New Issue
Block a user