Playwright icon

Playwright

Automate browser interactions using Playwright

Overview

This node allows you to run custom JavaScript scripts using the Playwright browser automation library within an n8n workflow. It provides access to a headless (or optionally headed) browser environment where you can programmatically navigate web pages, interact with them, and extract data.

A common use case is web scraping or automating interactions on websites that require JavaScript execution, such as logging in, clicking buttons, or extracting dynamic content. For example, you could write a script to visit an IP lookup service, extract your public IP address from the page, and return it as output.

The "Run Custom Script" operation specifically lets you write arbitrary Playwright code with access to special variables representing the browser ($browser), the current page ($page), and the Playwright library itself ($playwright). This flexibility enables complex automation scenarios tailored to your needs.

Properties

Name Meaning
Script Code JavaScript code to execute with Playwright. You have access to $browser, $page, and $playwright objects representing the Playwright browser and page. The script must return an array of items for output.
Options A collection of optional settings to control browser behavior and script execution:
  Batch Size Maximum number of pages to open simultaneously. Higher values increase memory and CPU usage.
  Browser WebSocket Endpoint WebSocket URL to connect to an existing browser instance instead of launching a new one.
  Emulate Device Emulate a specific device profile (viewport size, user agent, etc.) from a predefined list of devices.
  Executable Path Path to a bundled browser executable. Ignored if connecting via WebSocket endpoint.
  Extra Headers Additional HTTP headers to send with requests. Specify multiple name-value pairs.
  File Name File name to assign to binary outputs (applies only to screenshot or PDF operations, not relevant for custom script).
  Launch Arguments Extra command line arguments to pass when launching the browser. Ignored if connecting via WebSocket endpoint.
  Timeout Maximum navigation time in milliseconds. Not applicable to the "Run Custom Script" operation.
  Protocol Timeout Maximum time in milliseconds to wait for protocol responses.
  Wait Until When to consider navigation succeeded (load, domcontentloaded, networkidle). Not applicable to the "Run Custom Script" operation.
  Page Caching Enable or disable page-level caching. Defaults to true.
  Headless mode Whether to run the browser in headless mode (no visible UI). Defaults to true.
  Proxy Server Proxy server configuration string (e.g., localhost:8080, socks5://localhost:1080).
  Add Container Arguments Whether to add recommended browser launch arguments for container environments (e.g., --no-sandbox). Defaults to true.

Output

  • The node outputs an array of JSON objects.
  • Each item corresponds to one input item processed by the script.
  • The JSON structure depends entirely on what the custom script returns but must be an array of objects.
  • Binary data output is not applicable for the "Run Custom Script" operation since it focuses on executing arbitrary JavaScript and returning JSON results.
  • Example output based on the default script:
[
  {
    "ip": "123.45.67.89",
    ... // other fields from input item preserved
  }
]

Dependencies

  • Requires the Playwright library to be available in the environment.
  • Uses Chromium browser by default but can be configured to connect to an existing browser instance via WebSocket.
  • Optional device emulation relies on Playwright's built-in device descriptors.
  • If using proxy or custom executable path, those must be correctly configured.
  • Environment variables can influence behavior, e.g., executable path override or enabling console output.
  • No internal credential types are exposed; however, if your script makes authenticated HTTP requests, you should handle credentials securely within your script or workflow.

Troubleshooting

  • Common issues:

    • Script does not return an array: The custom script must return an array of objects. Returning anything else will cause an error.
    • Browser launch failure: Incorrect executable path, invalid WebSocket endpoint, or missing dependencies can prevent the browser from launching or connecting.
    • Memory/CPU overload: Setting batch size too high may exhaust system resources.
    • Invalid URLs or navigation errors inside the script can cause failures.
    • Proxy misconfiguration can block connections.
  • Error messages:

    • "Custom script must return an array of items...": Ensure your script ends with return [{...}] or similar.
    • "Failed to launch/connect to browser: ...": Check browser executable path, WebSocket URL, and environment setup.
    • "Invalid URL: ...": Verify URLs used in your script or parameters.
    • "No response received from the URL" or HTTP status >= 400: The target page might be down or inaccessible.
    • Errors closing pages or browser are logged but do not usually stop execution.
  • How to resolve:

    • Validate your script syntax and ensure it returns an array.
    • Confirm browser installation and paths.
    • Use console.log statements in your script to debug; logs appear in the workflow execution output or browser console depending on configuration.
    • Adjust batch size to reduce resource consumption.
    • Double-check proxy and network settings.

Links and References

Discussion