【问题标题】:Selenium NodeJS print cookies to consoleSelenium NodeJS 将 cookie 打印到控制台
【发布时间】:2016-07-17 11:47:31
【问题描述】:

我正在为 Node.JS 使用 selenium-webdriver npm 模块,但是我无法将 cookie 写入控制台。我使用了NPM page中的示例代码(正在使用中)

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

现在,我希望 console.log 可以编写我在其他问题中看到的字典,但是我得到以下输出:

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 }

我在控制台中没有收到任何错误,但我也没有收到预期的 cookie。我正在使用最新版本的节点 v5.9.1 和最新版本的 selenium-webdriver。出于某种原因,console.log 代码在 selenium 的 Firefox 实例启动之前被调用。我该如何解决这个问题?

【问题讨论】:

    标签: javascript node.js selenium cookies


    【解决方案1】:

    您可以使用 .then()。更多信息请参考When should we use .then with Protractor Promise?

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

    【讨论】:

      猜你喜欢
      • 2013-03-29
      • 1970-01-01
      • 2011-12-03
      • 1970-01-01
      • 1970-01-01
      • 2016-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多