Source code for langchain_community.tools.playwright.navigate
from __future__ import annotations
from typing import Optional, Type
from urllib.parse import urlparse
from langchain_core.callbacks import (
    AsyncCallbackManagerForToolRun,
    CallbackManagerForToolRun,
)
from langchain_core.pydantic_v1 import BaseModel, Field, validator
from langchain_community.tools.playwright.base import BaseBrowserTool
from langchain_community.tools.playwright.utils import (
    aget_current_page,
    get_current_page,
)
[docs]class NavigateToolInput(BaseModel):
    """Input for NavigateToolInput."""
    url: str = Field(..., description="url to navigate to")
    @validator("url")
    def validate_url_scheme(cls, url: str) -> str:
        """Check that the URL scheme is valid."""
        parsed_url = urlparse(url)
        if parsed_url.scheme not in ("http", "https"):
            raise ValueError("URL scheme must be 'http' or 'https'")
        return url
[docs]class NavigateTool(BaseBrowserTool):
    """Tool for navigating a browser to a URL.
    **Security Note**: This tool provides code to control web-browser navigation.
        This tool can navigate to any URL, including internal network URLs, and
        URLs exposed on the server itself.
        However, if exposing this tool to end-users, consider limiting network
        access to the server that hosts the agent.
        By default, the URL scheme has been limited to 'http' and 'https' to
        prevent navigation to local file system URLs (or other schemes).
        If access to the local file system is required, consider creating a custom
        tool or providing a custom args_schema that allows the desired URL schemes.
        See https://python.langchain.com/docs/security for more information.
    """