3

我想在Node.js中使用Selenium Webdriver Chrome進入網頁,填寫輸入,單擊按鈕,然後檢索瀏覽器控制檯的內容。我能夠獲得網頁,填寫輸入,然後單擊按鈕,但到目前爲止我無法弄清楚如何檢索控制檯的內容。我怎麼做?閱讀控制檯在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(); 

回答

2

documentation

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

謝謝你的鏈接!我花了下午的時間閱讀文檔,但從未找到該頁面。我現在意識到,當我在看日誌→selenium-webdriver/lib/logging(http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/index.html)我假定日誌記錄和selenium-webdriver/lib/logging鏈接都到了相同的地方(他們沒有),我只是點擊了日誌記錄鏈接。無論如何,這正是我正在尋找的文檔! – irrational

相關問題