python
import os
import tempfile
import shutil
TARGET_URL = "https://www.google.com"
TIMEOUT_SECONDS = 10
# Detect the operating sfrom playwright.sync_api import sync_playwright
import time
import platform
ystem
current_os = platform.system()
# Set browser path and user data directory based on OS
if current_os == "Darwin":
PB_BROWSER = "/Applications/Prisma Access Browser.app/Contents/MacOS/Prisma Access Browser"
# Always use explicit user-data-dir to avoid platform-specific issues
USER_DATA_DIR = os.path.join(tempfile.gettempdir(), "playwright-pab-userdata")
elif current_os == "Windows":
PB_BROWSER = r"C:\Program Files\Palo Alto Networks\PrismaAccessBrowser\Application\PrismaAccessBrowser.exe"
USER_DATA_DIR = os.path.join(os.getenv('TEMP'), "playwright-pab-userdata")
elif current_os == "Linux":
PB_BROWSER = "/usr/bin/prisma-access-browser"
USER_DATA_DIR = os.path.join(tempfile.gettempdir(), "playwright-pab-userdata")
else:
raise Exception(f"Unsupported operating system: {current_os}")
print(f"Detected OS: {current_os}")
print(f"Browser path: {PB_BROWSER}")
print(f"User data directory: {USER_DATA_DIR}")
def main():
start_time = time.time()
with sync_playwright() as p:
print("Starting Playwright Session...")
try:
context = p.chromium.launch_persistent_context(
user_data_dir=USER_DATA_DIR,
executable_path=PB_BROWSER,
headless=False,
args=[
# --remote-debugging-port=9222 # Uncomment to specify port explicitly. Assure the port is available.
],
)
page = context.new_page()
print(f"Navigating to {TARGET_URL}...")
page.goto(TARGET_URL)
page.wait_for_load_state('networkidle')
print(f"Page loaded successfully.")
print(f"Page title: {page.title()}")
print(f"Waiting for {TIMEOUT_SECONDS} seconds...")
time.sleep(TIMEOUT_SECONDS)
end_time = time.time()
elapsed_time = end_time - start_time
print(f"\nTimeout completed.")
print("Closing browser...")
except Exception as e:
print(f"Error: {e}")
raise
# Cleanup after playwright context is closed
print(f"Cleaning up user data directory: {USER_DATA_DIR}")
# Wait for browser to fully release file locks
time.sleep(2)
try:
shutil.rmtree(USER_DATA_DIR)
print("✓ User data directory deleted successfully")
except Exception as e:
print(f"✗ Could not delete directory: {e}")
print(f" Manual cleanup required: {USER_DATA_DIR}")
print("Browser session closed. Script ended.")
if __name__ == "__main__":
main()