使用Node.js上的Selenium Webdriver Chrome阅读控制台

我想在Node.js中使用Selenium Webdriver Chrome进入一个网页,填写一个input,单击一个button,然后检索浏览器控制台的内容。 我能够得到网页,填写input,然后单击button,但到目前为止我无法弄清楚如何检索控制台的内容。 我怎么做?

这是迄今为止的代码:

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

根据文档 :

 driver.manage().logs().get(logging.Type.BROWSER) .then(function(entries) { entries.forEach(function(entry) { console.log('[%s] %s', entry.level.name, entry.message); }); }); 
Interesting Posts