5,000+ Questions

💡 If this helped you, pay it forward. Share CareerRaah with a QA friend or your LinkedIn network — it's completely free and it might just change their career. 🚀

Legacy to Agentic

Selenium POM Wrapper

Zero code rewrite. Learn how to wrap your existing, legacy Java and Python Selenium Page Object Models (POMs) into AI-accessible tools.

The Wrapper Concept

You don't have to throw away years of Selenium framework investments. By exposing your high-level Page Object methods (e.g. `loginUser`, `verifyDashboard`) through a lightweight MCP Python/Java wrapper, an AI agent can orchestrate those complex actions without knowing how to write Selenium code.

Selenium MCP Wrapper

Step-by-Step Wrapper Implementation

You can expose your existing Selenium Page Object Model suite to AI agents using either the official **TypeScript/Node.js stack** or a standard **Python-based FastMCP stack**. Explore the complete guides for both options below.

⚡ Legacy Framework Note

Because Selenium is a legacy testing engine, there is no official pre-built zero-code MCP server. However, you can run our pre-built TypeScript wrapper (Option A) or Python wrapper (Option B) code with a simple copy-paste to run them in seconds!

Option ATypeScript: Node.js Selenium MCP Setup

Under Node.js, you can build a customized Selenium tool wrapper using the official @modelcontextprotocol/sdk and the native selenium-webdriver library.

Step 1: Install NPM Packages

Initialize a Node project and install the MCP SDK and Selenium drivers:

npm init -y
npm install @modelcontextprotocol/sdk selenium-webdriver
npm install --save-dev typescript @types/node tsx

Step 2: Build the Server

Write a custom script `mcp_selenium_server.ts` to manage WebDriver instances asynchronously:

mcp_selenium_server.ts (TypeScript)
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";
import { Builder, WebDriver, By } from "selenium-webdriver";

let driver: WebDriver | null = null;

const server = new Server(
    { name: "selenium-mcp-ts", version: "1.0.0" },
    { capabilities: { tools: {} } }
);

// 1. Expose standard Page Object Actions as AI Tools
server.setRequestHandler(ListToolsRequestSchema, async () => {
    return {
        tools: [
            {
                name: "execute_selenium_login",
                description: "Logs user in using wrapped legacy TS Selenium selectors.",
                inputSchema: {
                    type: "object",
                    properties: {
                        username: { type: "string" },
                        password: { type: "string" }
                    },
                    required: ["username", "password"]
                }
            }
        ]
    };
});

// 2. Handle execution of the Selenium tool
server.setRequestHandler(CallToolRequestSchema, async (request) => {
    const { name, arguments: args } = request.params;
    if (!driver) {
        driver = await new Builder().forBrowser("chrome").build();
    }
    if (name === "execute_selenium_login") {
        const username = args?.username as string;
        const password = args?.password as string;
        
        await driver.get("https://portal.careerraah.com/login");
        await driver.findElement(By.id("username")).sendKeys(username);
        await driver.findElement(By.id("password")).sendKeys(password);
        await driver.findElement(By.css("button[type='submit']")).click();
        
        const title = await driver.getTitle();
        return {
            content: [{ type: "text", text: `Selenium login action triggered. Current browser title: "${title}"` }]
        };
    }
    throw new Error("Tool execution failed.");
});

async function run() {
    const transport = new StdioServerTransport();
    await server.connect(transport);
}
run().catch(console.error);

Option BPython: Legacy Selenium Wrapper Setup

Alternatively, you can build your server using Python's high-level fastmcp framework to wrap your existing Python Selenium POM classes.

Step 1: Install Python Libraries

Install FastMCP and Selenium dependencies:

pip install fastmcp selenium webdriver-manager

Step 2: Build the Server

Write `selenium_mcp_server.py` to wrap legacy Python page object models:

selenium_mcp_server.py (Python)
from fastmcp import FastMCP
from selenium import webdriver
# Import your existing legacy POM
from pages.login_page import LoginPage

server = FastMCP("Legacy-Selenium-Wrapper")

# Keep WebDriver instance alive in global state
driver = webdriver.Chrome()

@server.tool()
def execute_login(username: str, password: str) -> str:
    """Logs the user into the portal using legacy Selenium POM."""
    login_page = LoginPage(driver)
    login_page.navigate_to_login()
    login_page.enter_credentials(username, password)
    login_page.click_submit()
    
    if "Dashboard" in driver.title:
        return "Login successful. Reached dashboard."
    else:
        return "Login failed."

if __name__ == "__main__":
    server.run(transport="stdio")

How AI Helps Generate POMs

What if you don't have a POM yet? You can use an agent connected to a pure DOM-reading MCP tool to look at your application, extract stable IDs, and write the Java/Python Selenium Page Object Model class for you.

Agent reads `<input id="user_123" />` via MCP
Agent writes `@FindBy(id="user_123") WebElement user;`