简体   繁体   中英

Selenium NodeJS print cookies to console

I'm using the selenium-webdriver npm module for Node.JS, however I am having trouble writing the cookies to the console. I used the example code from the NPM page (Under usage)

var webdriver = require('selenium-webdriver'),
    By = webdriver.By,
    until = webdriver.until;

var driver = new webdriver.Builder()
    .forBrowser('firefox')
    .build();

driver.get('http://www.google.com/ncr');
driver.findElement(By.name('q')).sendKeys('webdriver');
driver.findElement(By.name('btnG')).click();
driver.wait(until.titleIs('webdriver - Google Search'), 1000);
console.log(driver.manage().getCookies());
driver.quit();

Now, I would expect console.log to write the dictionary I've seen referenced in other questions, however I get the following output:

ManagedPromise {
  flow_: 
   ControlFlow {
     propagateUnhandledRejections_: true,
     activeQueue_: 
      TaskQueue {
        name_: 'TaskQueue::5',
        flow_: [Circular],
        tasks_: [Object],
        interrupts_: null,
        pending_: null,
        state_: 'new',
        unhandledRejections_: Set {} },
     taskQueues_: Set { [Object] },
     shutdownTask_: null,
     hold_: 
      Timeout {
        _called: false,
        _idleTimeout: 2147483647,
        _idlePrev: [Object],
        _idleNext: [Object],
        _idleStart: 231,
        _onTimeout: [Function: wrapper],
        _repeat: [Function] } },
  stack_: { [Task: WebDriver.manage().getCookies()] name: 'Task' },
  parent_: null,
  callbacks_: null,
  state_: 'pending',
  handled_: false,
  value_: undefined,
  queue_: null }

I get no errors in my console, but I'm not getting the cookies I expected either. I'm using the latest version of node, v5.9.1, and the latest version of selenium-webdriver. For some reason the console.log code is called before selenium's instance of Firefox even starts. How would I go about fixing this?

You can use .then(). Refer to When should we use .then with Protractor Promise? for more information.

driver.manage().getCookies().then(function (cookies) {
    console.log(cookies);
}); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM