5

我想在 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();
4

1 回答 1

7

根据文档

driver.manage().logs().get(logging.Type.BROWSER)
    .then(function(entries) {
        entries.forEach(function(entry) {
          console.log('[%s] %s', entry.level.name, entry.message);
        });
     });
于 2017-10-18T01:35:52.170 回答