获取控制台日志错误代码 selenium js

Fetch console logs error code selenium js

以下是我在selenium JS中的代码,在最后一步,我想从浏览器控制台中获取错误代码,我需要检查一下,浏览器控制台中应该没有任何504错误代码当前页面。

driver.get(M_URL)
    .then(() => {
        return driver.findElement(By.xpath('//input[@id="UserName"]'))
            .then(el => el.sendKeys(USERNAME));
    })
    .then(() => {
        return driver.findElement(By.xpath('//input[@id="Password"]'))
            .then(el => el.sendKeys(PASSWORD));
    })
    .then(() => {
        return driver.findElement(By.xpath('//button[text()="Login"]'))
            .then(el => el.click());
    })
    .then(() => {
    return driver.findElement(By.xpath('//h3[text()[contains(.,"Publisher")]]')).click()
        .then(() => log('Publisher page is rendered'));
    })
    .then(() => log('check 504 error code in browser console'));

提前致谢!

这里是你如何做到的

require('chromedriver');

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

var builder = new wd.Builder();
var options = new chrome.Options();
var prefs = new wd.logging.Preferences();
var driver;

prefs.setLevel(wd.logging.Type.BROWSER, wd.logging.Level.ALL);
options.setLoggingPrefs(prefs);

driver = builder
    .forBrowser(wd.Browser.CHROME)
    .setChromeOptions(options)
    .build();

driver
    .get(`file://${path.resolve(__dirname, './page.html')}`)
    .then(() => driver.manage().logs().get(wd.logging.Type.BROWSER))
    .then((logs) => {
        console.log(logs);
    })
    .then(() => driver.quit());

更多信息here