Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading Console using Selenium Webdriver Chrome on Node.js

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();
like image 656
irrational Avatar asked Oct 18 '17 00:10

irrational


1 Answers

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);
        });
     });
like image 181
Lucas Tierney Avatar answered Oct 31 '22 22:10

Lucas Tierney