update: wait until body attached in playwright-service

This commit is contained in:
youqiang 2024-05-21 14:53:57 +08:00
parent 01783dc336
commit c47dae13a9

View File

@ -5,6 +5,7 @@ from pydantic import BaseModel
app = FastAPI() app = FastAPI()
class UrlModel(BaseModel): class UrlModel(BaseModel):
url: str url: str
wait: int = None wait: int = None
@ -29,9 +30,12 @@ async def shutdown_event():
async def root(body: UrlModel): async def root(body: UrlModel):
context = await browser.new_context() context = await browser.new_context()
page = await context.new_page() page = await context.new_page()
await page.goto(body.url, timeout=15000) # Set max timeout to 15s await page.goto(
if body.wait: # Check if wait parameter is provided in the request body body.url,
await page.wait_for_timeout(body.wait) # Convert seconds to milliseconds for playwright wait_until="load",
timeout=body.wait if body.wait else 15,
)
await page.wait_for_selector("body", state="attached")
page_content = await page.content() page_content = await page.content()
await context.close() await context.close()
json_compatible_item_data = {"content": page_content} json_compatible_item_data = {"content": page_content}