I have spent a while for the solution but couldn't find anything for my specific problem. I am automating a task with Selenium and need to log in a system. If I do it manually, the system will keep the user logged if a new browser tab or window is opened, but with Selenium, this doesn't happen if it is the second run of the program. It will open a new tab and won't even navigate to the url on the code. Is there any way to make the browser reuse the user session from the previous Selenium run? I'm using Java for the automation.


Apologies, but I wasn't very clear. I looked for a way to use the opened browser before asking this question, as the question marked as a duplicate of mine asks, but as it doesn't seem feasible I asked about another approach. The thing is an opened browser is called session, but I mean a session when a user is logged in a system and opens a new browser tab or window and can navigate to the same system without logging in again. As I mentioned, on the second run of a Selenium Java program it won't navigate to the same system url nor use the same user session.

  • Many thanks @Leon. I need this to avoid logging in the system every time I run the automation code. It is not for testing, it is for automating repetitive tasks. As it seems too difficult to reuse an open browser for the second run of the program, I'm looking for a way to open a new browser or tab running the program multiple times but using that same user session already logged in the system. I could not find anything about that on the specifications. Could you please be a bit more specific?
    – Marco.S
    Sep 12, 2017 at 18:04

3 Answers 3


Yes, that is easily doable. What you need to do is get the cookies your browser received from the website and keep them in a persistent storage such as a text file or a database.

Beware that due to security reasons, namely Same-origin policy, your webdriver must be in the domain where the cookie was originaly generated to be set, otherwise, it is discarded - https://stackoverflow.com/a/28331099/705147

This little article should get you started: http://www.qaautomationsimplified.com/selenium/selenium-webdriver-get-cookies-from-an-existing-session-and-add-those-to-a-newly-instantiated-webdriver-browser-instance/


First of welcome to SQA SE. Now instead of trying to delve into setup for webdriver's specifics, you can either do all actions in a monolithic fashion with the same session or (as recommended) actually use java as an object oriented language and create classes/methods appropriately to assist your developing and maximize re-usable code, for example the login code.

If for some reason you absolutely need a session to be shared for what you try to achieve,and its not just a case of bad code design that led you to this need, you can take a look at the webdriver specifications for more info.


Declare the driver as static in super class and use it whenever required without initiating it again:

Public class library() {

    public static WebDriver driver;

    public static void launchBrowser(String BrowserName) throws IOException {

        driver = new FirefoxDriver();

    @Test(priority = 1)
    public void openApplication() throws Throwable {

    @Test(priority = 2)
    public void enterCredential() throws Throwable {
        userName("id", "login:login_box:username", TestData.username);
        passWord("xpath", "//*[@id='login:login_box:password']", TestData.passWord);

Your script will continue with one instance of the browser.

  • Thank you @Rita, but as I mentioned on my question and a comment, I'm not doing tests and need to run the code multiple times, so a static driver won't help me.
    – Marco.S
    Sep 13, 2017 at 12:05
  • @Marco, what I understood from your comment : User will login once and use the same session/user for all the task. Solution: Write code for user login and then add all your next code/actions in "for loop" as per your requirement. You can close the window within the For loop but quit the driver outside the For loop or at the end of the program execution.
    – Rita
    Sep 14, 2017 at 6:02
  • Thank you @Rita, but I need to run the program multiple times, so this approach won't work, because on the second run Selenium will open Chrome and won't navigate to the website already opened. This is different than when you're using a browser manually and open another tab or window, go to the website and it will load within the same user session. This is what I don't know.
    – Marco.S
    Sep 14, 2017 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.