{"flag":true,"single":true,"pageTitle":"Authentication","post":{"id":294,"user_id":"1","slug":"authentication-kerr","title":"Authentication","body":"<p><strong>Authentication <\/strong>is used to store login data to a file and then reuse it &nbsp;<a href=\"https:\/\/playwright.dev\/python\/docs\/auth\">https:\/\/playwright.dev\/python\/docs\/auth<\/a><\/p>\r\n<pre class=\"language-markup\"><code> context.storage_state(path=auth_file) # store files to authentication<\/code><\/pre>\r\n<p>1. saveauth.py and run this file to save auth<\/p>\r\n<pre class=\"language-markup\"><code>from playwright.sync_api import sync_playwright\r\nimport os\r\n\r\ndef setup_auth():\r\n    with sync_playwright() as p:\r\n        browser = p.chromium.launch( headless=False)\r\n        context = browser.new_context()\r\n        page = context.new_page()\r\n        page.goto('https:\/\/github.com\/login') # Navigate to GitHub login\r\n\r\n        # Interact with login form\r\n        page.get_by_label(\"Username or email address\").fill(\"RizwanKMW\")\r\n        page.get_by_label(\"Password\").fill(\"Password here\")\r\n        page.get_by_role(\"button\", name=\"Sign in\", exact=True).click()\r\n\r\n        # Wait for navigation to complete\r\n        page.wait_for_url('https:\/\/github.com\/')\r\n\r\n        # Define and check auth directory\r\n        auth_dir = 'auth\/.auth'\r\n        auth_file = os.path.join(auth_dir, 'github-auth.json')\r\n        \r\n        # Verify directory exists and is writable\r\n        if not os.path.exists(auth_dir):\r\n            print(f\"Creating directory: {auth_dir}\")\r\n            os.makedirs(auth_dir)\r\n        else:\r\n            print(f\"Directory already exists: {auth_dir}\")\r\n            \r\n        # Check if we have write permissions\r\n        if not os.access(auth_dir, os.W_OK):\r\n            raise PermissionError(f\"No write permission for directory: {auth_dir}\")\r\n            \r\n        print(f\"Attempting to save state to: {os.path.abspath(auth_file)}\")\r\n        \r\n        # Save the authenticated state\r\n        context.storage_state(path=auth_file)\r\n        \r\n        print(\"Authentication state saved successfully\")\r\n        browser.close()\r\n\r\nif __name__ == '__main__':\r\n    setup_auth()<\/code><\/pre>\r\n<p>2. reuse auth<\/p>\r\n<pre class=\"language-markup\"><code>from playwright.sync_api import sync_playwright\r\nimport os\r\nimport time\r\n\r\ndef verify_auth_state():\r\n    # Path to your saved auth state\r\n    auth_file = 'auth\/.auth\/github-auth.json'\r\n    \r\n    # Verify auth file exists\r\n    if not os.path.exists(auth_file):\r\n        print(f\"Authentication state file not found at: {os.path.abspath(auth_file)}\")\r\n        print(\"Please run setup_auth.py first\")\r\n        return\r\n\r\n    try:\r\n        with sync_playwright() as p:\r\n            # Launch browser in non-headless mode with saved state\r\n            print(f\"Loading authentication state from: {os.path.abspath(auth_file)}\")\r\n            browser = p.chromium.launch(headless=False, slow_mo=500)  # slow_mo adds delay for visibility\r\n            context = browser.new_context(storage_state=auth_file)\r\n            page = context.new_page()\r\n\r\n            # Go to a protected GitHub page\r\n            print(\"Navigating to GitHub settings page...\")\r\n            page.goto('https:\/\/github.com\/settings\/profile')\r\n            \r\n            # Print current URL to verify where we landed\r\n            print(f\"Current URL: {page.url}\")\r\n            \r\n            # Check if we're logged in by looking for profile elements\r\n            try:\r\n                profile_header= page.locator(\"textarea[class='form-control form-control user-profile-bio-field js-length-limited-input']\")\r\n                if profile_header.is_visible(timeout=10000):\r\n                    print(\"Successfully authenticated - Profile page visible\")\r\n                    profile_header.fill(\"Zindgi dii chass lay gay o\")\r\n                else:\r\n                    print(\"Authentication might have failed - Profile header not found\")\r\n            except Exception as e:\r\n                print(f\"Error checking profile header: {str(e)}\")\r\n                print(f\"Page content preview: {page.content()[:500]}...\")\r\n\r\n            # Keep browser open for manual inspection\r\n            print(\"Browser will remain open for 60 seconds. Interact with it manually if needed.\")\r\n            time.sleep(60)  # Keeps browser open for 60 seconds\r\n            input()\r\n            # You can uncomment below to keep it open indefinitely until you close it manually\r\n            # input(\"Press Enter to close the browser...\")\r\n\r\n            browser.close()\r\n            print(\"Browser closed\")\r\n\r\n    except Exception as e:\r\n        print(f\"Error occurred: {str(e)}\")\r\n\r\nif __name__ == '__main__':\r\n    verify_auth_state()<\/code><\/pre>","category_id":"37","is_private":"0","created_at":"2025-03-05T23:59:47.000000Z","updated_at":"2025-03-05T23:59:47.000000Z","category":{"id":37,"user_id":"1","name":"Playwright","slug":"playwright-c2zf","parent_id":null,"created_at":"2025-03-05T22:35:12.000000Z","updated_at":"2025-03-05T22:35:12.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"Authentication is used to store login data to a file and then reuse it &nbsp;https:\/\/playwright.dev\/python\/docs\/auth  context.storage_state( - Authentication (Updated: March 5, 2025) - Read more about Authentication at my programming site [SITE]","categories":[]}