0

Im creating a browser with selenium in nodejs

but i need save session/cookie WHEN window close

i tried it:

const { Builder } = require('selenium-webdriver');
const fs = require('fs');

async function example() {
  let driver = await new Builder()
  .forBrowser('firefox')
  .build();
  await driver.get('http://www.facebook.com');

  // fs.appendFile('server.log', driver.manage().getCookies().toString(), function (err) {
  //   if (err) return console.log(err);
  //   console.log('Appended!');
  // });
}

example();

but its open and save, i need save WHEN browser is closed, i thought in something like: driver.addEventListener('on-close', function() { fs.appendFile('server.log', driver.manage().getCookies().toString(), function (err) { // if (err) return console.log(err); // console.log('Appended!'); }) but i dont found nothing about "when close"

Thanks for help =)

Jhon
  • 1
  • Try [localStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) – Jay Nyxed Jun 17 '20 at 21:45
  • yeah, but my ask is: how can i get this event "when-close" ? – Jhon Jun 17 '20 at 21:50
  • Then [this](https://stackoverflow.com/questions/13443503/run-javascript-code-on-window-close-or-page-refresh) should help, sorry for misunderstanding =) – Jay Nyxed Jun 17 '20 at 21:51
  • i tried but dont work =( – Jhon Jun 17 '20 at 22:02
  • I think its a time issue. I tried to setTimeout on my test page and it didn't save anything when I closed the page. Maybe if its taking that much time, then it's better to save cookies every time something changes? – Jay Nyxed Jun 17 '20 at 22:16

1 Answers1

0

Try this:

driver.quit(function())