Reading Console using Selenium Webdriver Chrome on Node.js
Asked Answered
W

2

6

I want to use Selenium Webdriver Chrome in Node.js to go to a web page, fill out an input, click a button and then retrieve the contents of the browser's console. I am able to get the web page, fill out the input, and click the button, but so far I can't figure out how to retrieve the contents of the console. How do I do that?

This is the code I have so far:

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();
Whilst answered 18/10, 2017 at 0:55 Comment(0)
H
9

Per the documentation:

driver.manage().logs().get(logging.Type.BROWSER)
    .then(function(entries) {
        entries.forEach(function(entry) {
          console.log('[%s] %s', entry.level.name, entry.message);
        });
     });
Hexylresorcinol answered 18/10, 2017 at 1:35 Comment(3)
Thank you for the link! I spent the afternoon reading the documentation, but never found that page. I now realize that when I was looking at logging → selenium-webdriver/lib/logging (on page seleniumhq.github.io/selenium/docs/api/javascript/module/…) I assumed that the logging and selenium-webdriver/lib/logging links both went to the same place (they don't) and I only ever clicked on the logging link. Anyway, that is exactly the documentation I was looking for!Whilst
Is it possible to get the logs of a custom fragment? I tried your first snippet after my driver.get("localhost:8080#htmlElementAction") and empty entries are obtained. ThanksAbukir
Broken link as of May 2024.Scourings
D
0

For everyone who can't get the logs to work, here's what worked for me:

this.driver = await new Builder()
        .forBrowser('chrome')
        .setChromeOptions(chromeOptions)
        .setCapability('goog:loggingPrefs', { 'browser':'ALL' })
        .build();

and then, after executing the script you want to get logs from run:

this.driver.manage().logs().get(logging.Type.BROWSER).then(function(text) {
        console.log('--CHROME LOGS--', text);
    });
Donny answered 16/6, 2022 at 12:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.