Simplex exposes a Playwright instance if you’ve installed the simplex[playwright] package.

Connecting to the Simplex session with Playwright

from simplex import Simplex

simplex = Simplex(api_key="your_api_key")
simplex.create_session() # creates the simplex.connect_url internally
pw_browser = simplex.pw.chromium.connect_over_cdp(simplex.connect_url)
page = pw_browser.contexts[0].pages[0]

Sample Usage

Here’s an involved example where we’re looking to retrieve the graphql authorization token from a page.

from simplex import Simplex
import asyncio

# create the Simplex session
simplex = Simplex(api_key="your_api_key")
simplex.create_session()
simplex.goto("website-with-graph-ql-auth")

# define the playwright browser instance
pw_browser = simplex.pw.chromium.connect_over_cdp(simplex.connect_url)
page = pw_browser.contexts[0].pages[0]

# define the auth event we're looking for
auth_event = asyncio.get_event_loop().create_future()

# define the response handler
async def handle_response(response, auth_event):
    if response.request.url.startswith("https://graphql"):
        try:
            auth_token = await response.request.header_value("authorization")
            if auth_token and not auth_event.done():
                # set the auth event for the first authorization response we find
                auth_event.set_result(auth_token)
        except Exception as e:
            print(f"Error handling response: {str(e)}")

# register the response handler
page.on("response", lambda response: handle_response(response, auth_event))

# reload the page to trigger the auth network request
page.reload()

# wait for the auth response
result = await asyncio.wait_for(auth_event, timeout=60)
print(result)