-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave_linkedin_session.py
More file actions
38 lines (24 loc) · 1.06 KB
/
Copy pathsave_linkedin_session.py
File metadata and controls
38 lines (24 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""Create a reusable LinkedIn Playwright session state file.
Use this locally (headless=False) to complete login + 2FA once, then save
session cookies/local storage for CI reuse.
"""
from __future__ import annotations
import os
from dotenv import load_dotenv
from playwright.sync_api import sync_playwright
load_dotenv()
def save_session_state() -> None:
output_path = os.getenv("LINKEDIN_STATE_FILE", "linkedin_state.json").strip()
with sync_playwright() as p:
browser = p.chromium.launch(headless=False, slow_mo=120)
context = browser.new_context()
page = context.new_page()
page.goto("https://www.linkedin.com/login", wait_until="domcontentloaded")
print("Complete LinkedIn login and any 2FA in the opened browser.")
input("After you can see your LinkedIn feed, press Enter here to save session... ")
context.storage_state(path=output_path)
print(f"Saved LinkedIn session state to: {output_path}")
context.close()
browser.close()
if __name__ == "__main__":
save_session_state()