如何在Puppeteer中接受JavaScript警报弹出窗口?

时间:2019-05-23 03:36:39

标签: javascript node.js automation puppeteer

我有一个脚本,该脚本使用Puppeteer自动登录公司门户。登录使用SAML。因此,当操纵up打开一个铬实例并访问该页面时,屏幕上会出现一个弹出窗口,以确认用户的身份。我需要做的就是手动单击“确定”按钮或按键盘上的Enter。

我曾尝试使用puppeteer模拟Enter键的按下,但这不起作用。

登录屏幕-

Login screen

脚本-

InternetExplorerOptions ieOptions = new InternetExplorerOptions();
ieOptions.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
ieOptions.IgnoreZoomLevel = true;
ieOptions.BrowserCommandLineArguments.Insert(0,"-private");
var _driver64 = new InternetExplorerDriver($"{Directory.GetCurrentDirectory()}", ieOptions);
_driver64.Manage().Window.Maximize();
_driver64.Navigate().GoToUrl("https://www.mypage.com");

**编辑**

Certificate export

1 个答案:

答案 0 :(得分:0)

this issue中提出了一种解决方案:

  

基本上只是拦截请求,然后使用您最喜欢的httpclient lib触发请求,并使用响应信息来响应被拦截的请求。

const puppeteer = require('puppeteer');
const request = require('request');
const fs = require('fs');

(async () => {
    const browser = await puppeteer.launch();
    let page = await browser.newPage();

    // Enable Request Interception
    await page.setRequestInterception(true);

    // Client cert files
    const cert = fs.readFileSync('/path/to/cert.crt.pem');
    const key = fs.readFileSync('/path/to/cert.key.pem');

    page.on('request', interceptedRequest => {
        // Intercept Request, pull out request options, add in client cert
        const options = {
            uri: interceptedRequest.url(),
            method: interceptedRequest.method(),
            headers: interceptedRequest.headers(),
            body: interceptedRequest.postData(),
            cert: cert,
            key: key
        };

        // Fire off the request manually (example is using using 'request' lib)
        request(options, function(err, resp, body) {
            // Abort interceptedRequest on error
            if (err) {
                console.error(`Unable to call ${options.uri}`, err);
                return interceptedRequest.abort('connectionrefused');
            }

            // Return retrieved response to interceptedRequest
            interceptedRequest.respond({
                status: resp.statusCode,
                contentType: resp.headers['content-type'],
                headers: resp.headers,
                body: body
            });
        });

    });

    await page.goto('https://client.badssl.com/');
    await browser.close();
})();