Access Logged In Sessions with Headless Chrome
In this article
To access a logged-in session with headless Chrome, you must point the browser to a specific User Data Directory where your session cookies and local storage are stored. By default, headless Chrome starts with a clean slate (a temporary profile), which is why you are logged out of websites. By using the --user-data-dir flag, you instruct Chrome to load an existing profile containing your authentication tokens, allowing your automation scripts to bypass login screens and access protected content immediately.
What You'll Need
- Google Chrome installed on your machine.
- A programming environment (Node.js with Puppeteer or Python with Selenium/Playwright).
- The file path to your Chrome User Data folder.
- A dedicated Chrome profile created specifically for automation (recommended).
Step-by-Step
- Create a Dedicated Automation Profile: Open your standard Chrome browser. Click on the Profile icon in the top right and select "Add". Create a new profile named "Automation". This prevents your automation script from interfering with your primary browsing session and avoids "Profile in use" errors.
- Log In Manually: Using this new "Automation" profile, navigate to the website you wish to access. Log in with your credentials, complete any Multi-Factor Authentication (MFA), and ensure the "Remember Me" box is checked. Close the browser completely once the session is established.
- Locate the User Data Directory: You need the path to the folder where Chrome stores this profile.
- Windows: Typically
C:\Users\<YourUsername>\AppData\Local\Google\Chrome\User Data - macOS: Typically
~/Library/Application Support/Google/Chrome - Linux: Typically
~/.config/google-chrome
- Windows: Typically
- Configure Your Automation Script: Add the
--user-data-dirargument to your browser launch configuration.- Puppeteer (Node.js): Use
puppeteer.launch({ args: ['--user-data-dir=/path/to/your/profile'] }). - Selenium (Python): Use
options.add_argument("--user-data-dir=/path/to/your/profile").
- Puppeteer (Node.js): Use
- Specify the Profile Directory: If you are using a profile other than "Default", you must also add the
--profile-directoryflag. For example, if your profile is "Profile 1", add--profile-directory="Profile 1"to your arguments. - Launch in Headless Mode: Set your headless flag to true. Because the browser is now loading your session data from the disk, it will open the target URL already authenticated.
Common Mistakes
- Leaving Chrome Open: Chrome does not allow two separate processes to access the same User Data Directory simultaneously. If your manual browser window is still open, the headless instance will fail to launch or will crash.
- Incorrect Pathing: Using relative paths instead of absolute paths often leads to the script creating a new, empty directory in the project folder rather than using the actual Chrome profile.
- Wrong Profile Folder: Pointing the
--user-data-dirto the root "User Data" folder but forgetting the--profile-directoryflag, which causes Chrome to default to the "Default" profile instead of the one where you logged in. - Ignoring Cookie Expiration: Assuming the session will last forever. Sessions eventually expire, and you will need to relaunch the browser in headed mode to re-authenticate manually.
Tips
- Use a Separate Folder: Instead of using your main Chrome installation path, copy the profile folder to your project directory. This makes your automation more portable and prevents accidental deletion of your main browser data.
- User-Agent Spoofing: Some websites detect headless Chrome by checking the User-Agent string. To avoid being flagged, set a real browser User-Agent in your script options.
- Disable Automation Flags: Use the argument
--disable-blink-features=AutomationControlledto hide the "Chrome is being controlled by automated test software" notification, which some sites use to trigger bot detection. - Session Persistence: Periodically check if the session is still active by scripting a check for a specific element that only appears when logged in.
Troubleshooting
If your headless browser is still appearing as a logged-out guest, check the following:
- Verify the Path: Print the path to your console in your script to ensure it is resolving correctly to the actual folder on your hard drive.
- Check for Lock Files: Look for a file named "SingletonLock" in the profile directory. If Chrome crashed previously, this file might remain and prevent the new session from loading the profile. Delete it and try again.
- Test in Headed Mode First: Change
headless: truetoheadless: false. If the browser opens and you are not logged in, the issue is with your path or profile directory. If you are logged in in headed mode but not in headless mode, the website is likely detecting the headless flag and clearing your cookies. - Update Chrome: Ensure your browser version and your automation library (Puppeteer/Selenium) are compatible. Version mismatches can cause profile loading failures.
Frequently Asked Questions
Can I use this to bypass 2FA?
Yes, because you perform the 2FA manually in a headed browser first. The resulting session cookie is stored in the User Data Directory, which the headless browser then loads, effectively bypassing the need to re-authenticate.
Is this method secure?
It is only as secure as the machine it is on. Since the User Data Directory contains plaintext session cookies, anyone with access to those files can hijack your session. Never upload your profile folder to public repositories like GitHub.
Does this work on Linux servers (VPS)?
Yes, but you must first create the profile on a machine with a GUI, then upload the entire profile folder to your server and point the --user-data-dir flag to that uploaded path.
Will the website know I am using a headless browser?
Many websites can detect headless Chrome via the navigator.webdriver property or specific browser fingerprints. Using a custom User-Agent and disabling automation flags can reduce this detection.
What happens if the session expires?
The headless browser will simply load the login page. Your script should be designed to detect the login page and either alert you or trigger a manual re-authentication process.