美文网首页
js playwright 模拟点击包括操作 iframe,模拟

js playwright 模拟点击包括操作 iframe,模拟

作者: 川流不息attitude | 来源:发表于2024-10-21 17:51 被阅读0次
async function login(username, password) {
    let userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36";
    let browser, context, page
    try {
        const launchOptions = { headless: true, devtools: false, slowMo: 200 }
        browser = await chromium.launch(launchOptions);
        context = await browser.newContext({ userAgent: userAgent })
        page = await context.newPage()
        const initScript = "Object.defineProperty(navigator, 'webdriver', {get: () => undefined});"
        await page.addInitScript(initScript);
        await page.addInitScript(stealthJs);
        await page.goto('XXXX');
        const reg = "XXXXX"
        const frameSelector = { url: new RegExp(reg) }
        const frameLocator = page.frame(frameSelector);
        await pause(200);
        //判断元素是否存在,他页面可能没有这个按钮
        await frameLocator.locator("//*[@id=\"login-form\"]/div[6]/a").waitFor({ timeout: 2000 });
        const exist = await frameLocator.locator("//*[@id=\"login-form\"]/div[6]/a").isVisible();
        console.log("用户密码按钮存在与否" + exist)
        if (!exist) {
            await page.close();
            await browser.close();
            return result(1, { "userinfo": "" }, "元素不存在,稍后再试");
        }
        await frameLocator.locator("//*[@id=\"login-form\"]/div[6]/a").click();
        await pause(50);
        await frameLocator.locator("//*[@id=\"fm-login-id\"]").fill(username);
        await pause(150);
        await frameLocator.locator("//*[@id=\"fm-login-password\"]").fill(password);
        await pause(160);
        const position = { x: 13, y: 23 };
        await frameLocator.locator("//*[@id=\"login-form\"]/div[5]/div").click({ position: position });
        await pause(230);
        await frameLocator.locator("//*[@id=\"login-form\"]/div[4]/button").click();
        await pause(800);
        //await page.locator("//*[@id=\"root\"]/div/div[1]/div[1]/div/div").allTextContents().waitFor()
        const text = await page.locator("//*[@id=\"root\"]/div/div[1]/div[1]/div/div").allTextContents()
        // 如果只有账号信息 直接返回""
        console.log("用户信息:" + text)
        let cookies = await page.context().cookies();
        //let storageState = await page.context().storageState();
        let data = { "userinfo": text == "账号名称" ? "" : username, "cookies": cookies };
        await pause(250);
        return result(0, data, 'success');
    } catch (error) {
        console.error(error);
        return result(1, { "userinfo": "" }, error.stack);
    } finally {
        if (typeof (page) !== 'undefined') {
            await page.close();
        }
        if (typeof (context) !== 'undefined') {
            await context.close()
        }
        if (typeof (browser) !== 'undefined') {
            await browser.close();
        }
    }
}

相关文章

网友评论

      本文标题:js playwright 模拟点击包括操作 iframe,模拟

      本文链接:https://www.haomeiwen.com/subject/bjjldjtx.html