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(); 

    2 Answers 2

    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); }); }); 
    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!CommentedOct 18, 2017 at 2:45
    • 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. ThanksCommentedJul 15, 2021 at 5:56
    • Broken link as of May 2024.CommentedMay 14, 2024 at 23:46
    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); }); 

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.