在 Node.js 上使用 Selenium Webdriver Chrome 的阅读控制台

Reading Console using Selenium Webdriver Chrome on Node.js

我想在 Node.js 中使用 Selenium Webdriver Chrome 转到网页,填写输入,单击按钮,然后检索浏览器控制台的内容。我能够获取网页,填写输入,然后单击按钮,但到目前为止我还不知道如何检索控制台的内容。我该怎么做?

这是我目前的代码:

const webdriver = require('selenium-webdriver');
const chromeDriver = require('selenium-webdriver/chrome');
const path = require('chromeDriver').path;

const service = new chromeDriver.ServiceBuilder(path).build();
chromeDriver.setDefaultService(service);

const { By, until } = webdriver;

webdriver.promise.USE_PROMISE_MANAGER = false;

const CHROME_BIN_PATH = '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome';

const options = new chromeDriver.Options();
options.setChromeBinaryPath(CHROME_BIN_PATH);
options.addArguments(
    'headless',
    'disable-gpu',
);

const main = async () => {
    try{
        const driver = await new webdriver.Builder()
            .withCapabilities(webdriver.Capabilities.chrome())
            .forBrowser('chrome')
            .setChromeOptions(options)
            .build();


        await driver.get('http://example.com/some/path.html');
        await driver.findElement(By.name('name2')).sendKeys('webdriver');
        await driver.findElement(By.name('button2')).click();

        const title = await driver.findElement(By.css('title')).getAttribute('innerHTML');
        console.log({ title });

        const log = GET THE BROWSER'S CONSOLE LOG
        console.log(log);

        await driver.quit();
    } catch (error) {
        console.log(error);
    }
};

main();

根据 documentation:

driver.manage().logs().get(logging.Type.BROWSER)
    .then(function(entries) {
        entries.forEach(function(entry) {
          console.log('[%s] %s', entry.level.name, entry.message);
        });
     });