Advertisement
Guest User

Untitled

a guest
Feb 6th, 2025
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.68 KB | Source Code | 0 0
  1. from playwright.sync_api import sync_playwright, Error
  2.  
  3. def test_insecure_website_headless():
  4.     with sync_playwright() as p:
  5.         browser = p.chromium.launch()
  6.         context = browser.new_context(ignore_https_errors=True)
  7.         page = context.new_page()
  8.  
  9.         try:
  10.             page.goto("https://expired.badssl.com/", timeout=30000)  # Increased timeout
  11.  
  12.             # Check for specific error messages or elements related to SSL issues.
  13.             # This is more reliable than just checking the status code.
  14.             if "NET::ERR_CERT_DATE_INVALID" in page.content() or \
  15.                "Your connection is not private" in page.content() or \
  16.                page.locator("#error-information-popup-content").is_visible(): # Example - adjust to the error page's content
  17.                 print("SSL certificate error detected.")
  18.                 page.screenshot(path="expired_ssl_error.png", full_page=True) # full_page is useful
  19.                 print("Screenshot saved to expired_ssl_error.png")
  20.             else:
  21.                 print("Page loaded (unexpectedly, or different error).")
  22.                 page.screenshot(path="unexpected_page.png", full_page=True)
  23.  
  24.         except Error as e: # Catch Playwright specific errors
  25.             print(f"A Playwright error occurred: {e}")
  26.             page.screenshot(path="playwright_error.png", full_page=True)  # Capture even Playwright's errors
  27.         except Exception as e:  # Catch broader exceptions
  28.             print(f"A general error occurred: {e}")
  29.             page.screenshot(path="general_error.png", full_page=True)
  30.  
  31.         finally:
  32.             context.close()
  33.             browser.close()
  34.  
  35. test_insecure_website_headless()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement