Next-Gen App & Browser Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Convert Selenium Java tests into Playwright Python tests for pytest. Conversion runs in your browser.
A Selenium to Playwright converter is a tool that rewrites Selenium WebDriver tests as Playwright tests. This one takes Selenium tests written in Java with TestNG or JUnit and returns Playwright Python tests for pytest, with locators, waits, assertions, setup hooks and the surrounding Java logic already translated.
Teams move from Selenium to Playwright for its built-in waiting, faster runs and simpler setup; the trade-offs can be explored at Playwright vs Selenium. The slow part of the move is rewriting every test by hand.
TestMu AI built this converter to do the mechanical part: it maps each WebDriver call to its Playwright equivalent, turns TestNG and JUnit structure into pytest fixtures and parameters, and lists the few places where the two tools behave differently. For a full migration plan, read Selenium to Playwright migration. If you are moving a Cucumber suite instead, the Gherkin to KaneAI Converter turns feature files into KaneAI tests.
The converter follows the Playwright locators and assertions guides and the pytest plugin reference. The most common mappings:
| Selenium Java | Playwright Python |
|---|---|
driver.get(url) | page.goto(url) |
driver.findElement(By.id("email")) | page.locator("#email") |
driver.findElements(By.cssSelector(".item")) | page.locator(".item"), looped with .all() |
element.sendKeys(text) / click() / clear() | locator.fill(text) / click() / clear() |
element.getText() / isDisplayed() | locator.inner_text() / is_visible() |
WebDriverWait with ExpectedConditions | expect(locator).to_be_visible() and other auto-retrying checks |
Assert.assertEquals(driver.getTitle(), title) | expect(page).to_have_title(title) |
new Select(element).selectByVisibleText(label) | locator.select_option(label=label) |
new Actions(driver).moveToElement(el).perform() | locator.hover() |
driver.switchTo().frame("name") | page.frame_locator(...), used for the steps that follow |
driver.switchTo().alert().accept() | page.once("dialog", ...), registered before the click that opens it |
@Test method | test_ method that takes the pytest-playwright page fixture |
@BeforeMethod / @AfterMethod (or JUnit @BeforeEach / @AfterEach) | autouse pytest fixture, with yield before the teardown |
@DataProvider, @ValueSource, @CsvSource | @pytest.mark.parametrize |
Page object with @FindBy fields | Python class that builds its locators in __init__ |
Loops, if/else, try/catch, collections, streams | Python for, if/elif, try/except, lists, dicts, comprehensions |
By.id, By.name, By.className, By.cssSelector, By.xpath, By.linkText, By.partialLinkText and By.tagName all become selector strings for page.locator(). Assertions on page text, titles, URLs, attributes, visibility and element counts become expect() checks, which retry until they pass; other assertions become plain Python assert statements.
This TestNG test checks the login page of the TestMu AI eCommerce Playground, a demo store built for practicing test automation. Input:
public class LoginTest {
WebDriver driver;
WebDriverWait wait;
@BeforeMethod
public void setUp() {
driver = new ChromeDriver();
wait = new WebDriverWait(driver, Duration.ofSeconds(10));
driver.get("https://ecommerce-playground.lambdatest.io/index.php?route=account/login");
}
@Test
public void wrongPasswordShowsWarning() {
driver.findElement(By.id("input-email")).sendKeys("shopper@example.com");
driver.findElement(By.id("input-password")).sendKeys("wrong-pass");
driver.findElement(By.xpath("//button[text()='Login']")).click();
WebElement alert = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".alert-danger")));
Assert.assertTrue(alert.getText().contains("No match for E-Mail Address"));
}
@AfterMethod
public void tearDown() {
driver.quit();
}
}Output, ready for pytest:
import pytest
from playwright.sync_api import Page, expect
class TestLogin:
@pytest.fixture(autouse=True)
def set_up(self, page: Page):
page.goto("https://ecommerce-playground.lambdatest.io/index.php?route=account/login")
def test_wrong_password_shows_warning(self, page: Page):
page.locator("#input-email").fill("shopper@example.com")
page.locator("#input-password").fill("wrong-pass")
page.locator("//button[text()='Login']").click()
alert = page.locator(".alert-danger")
expect(alert).to_be_visible(timeout=10000)
expect(alert).to_contain_text("No match for E-Mail Address")The ChromeDriver setup and driver.quit() are gone, because the pytest-playwright page fixture opens and closes the browser. The @BeforeMethod became an autouse fixture, the explicit wait became an expect() check that keeps its 10 second timeout, and the assertion on the alert text became to_contain_text(), which retries until the text appears.
Paste a page object and the tests that use it together: the converter links them, so method calls such as loginPage.login() and the page object's locators are converted consistently.
Selenium and Playwright drive the browser in different ways, so a line-for-line translation is not always the same test. The converter flags each of these in the Check before you run list:
Once the converted suite passes locally, you can run it on Playwright testing in the cloud across browser versions, or speed up large suites with HyperExecute.
No. The Selenium to Playwright converter runs entirely in your browser and writes the Python on the same page, so your Java source, URLs, test data and credentials never leave your machine. Nothing is sent to a server, so you can convert tests for internal applications without exposing their addresses or accounts.
The converter reads full Java test classes written for TestNG, JUnit 4 or JUnit 5, page object classes with @FindBy fields, and plain snippets of WebDriver calls. It converts waits, assertions, Select, Actions, frames, alerts, JavaScript calls and general Java logic, and lists any line it could not convert so you can finish it by hand.
Playwright waits for each element before acting on it, so implicit waits are removed and WebDriverWait conditions become expect() assertions, which retry until they pass or time out. The converter keeps the timeout from your WebDriverWait. Thread.sleep becomes page.wait_for_timeout(), which still pauses, and is flagged so you can replace it with an assertion.
A TODO comment marks a line the converter could not translate with confidence, such as a third-party library call, Selenium's low-level pointer API or window handle switching. The original Java is described in the comment, and the same items appear in the Check before you run list with their line numbers, so nothing is dropped silently.
In pytest-playwright, the page fixture launches the browser, opens a fresh context for every test and closes it afterwards. That replaces new ChromeDriver(), browser options, window maximize and driver.quit(). You choose the browser when you run the tests, for example pytest --browser firefox, and add --headed to watch the run.
Mostly, yes. The Selenium to Playwright converter rewrites WebDriver calls, waits, assertions, TestNG and JUnit hooks and the surrounding Java logic into Playwright Python in one step. Parts with no direct equivalent, such as window handles or third-party libraries, are marked as TODOs, so review and run each converted test before retiring the Selenium version.
Did you find this page helpful?
TestMu AI forEnterprise
Get access to solutions built on Enterprise
grade security, privacy, & compliance