Notes
New tabs
ShopRung's footer link, SQL console, opens in a new tab. In Playwright, a new tab is a new page in the same context. Start waiting for the context's page event and click the link together, using promise all, so the event is not missed. Then wait for the new page to load. Our run caught it: the URL ending in slash sql, the title ShopRung SQL console, and two pages in the context. Work with both pages as normal objects.
const [sql] = await Promise.all([
context.waitForEvent("page"),
page.getByRole("link",
{ name: "SQL console" })
.click(),
]);
await sql.waitForLoadState();
real: url .../sql, title
"ShopRung SQL console",
2 pages in the context Tabs in Selenium
Selenium works with one window at a time, identified by handles. Save the main handle, click the link, and wait until there are two windows. Loop through the handles and switch to the one that is not the main window. Test it, close it, and switch back to the main handle. Forgetting to switch back is a classic cause of no such element errors on the next step.
String main = d.getWindowHandle();
d.findElement(By.linkText(
"SQL console")).click();
wait.until(numberOfWindowsToBe(2));
for (String h : d.getWindowHandles())
if (!h.equals(main))
d.switchTo().window(h);
// ... test the new tab
d.close();
d.switchTo().window(main); Uploads
Never try to drive the operating system's file picker. Set files directly on the file input. Playwright's set input files takes a path, several paths, or an in memory file with a name, type and contents, and an empty list clears it. On our demo page, the label receipt took receipt dot txt, and the page showed uploaded receipt dot txt, thirteen bytes. In Selenium, send the absolute file path as keys to the file input.
await page.getByLabel("Receipt")
.setInputFiles("receipt.txt");
await expect(page.locator("#nm"))
.toHaveText("Uploaded
receipt.txt (13 bytes)");
also: setInputFiles([a, b]),
{ name, mimeType, buffer },
setInputFiles([]) to clear
Selenium: input.sendKeys(
absolutePath) Downloads
For downloads, start waiting for the download event, click export orders, then await the download. Save it to a known path and check it: our run got the file name orders dot csv, and its content was a header row and one order of forty two ninety seven. Checking the content, not just that a file appeared, is what makes it a real test. Selenium needs a browser download directory set in its options, then you wait for the file to appear.
const dlP = page.waitForEvent(
"download");
await page.getByRole("link",
{ name: "Export orders" })
.click();
const dl = await dlP;
await dl.saveAs("out/orders.csv");
real: suggestedFilename
"orders.csv", content
"id,total\n1,42.97" iframes
An iframe is a separate document inside the page, common for payment forms, maps, videos and chat widgets. Locators on the main page cannot see inside it. Use a frame locator for the iframe, found here by its title, payment, then locate inside it as usual: fill the card name, click pay, and expect paid to be visible. Our run passed. Locating the card field from the main page finds nothing.
const pay = page.frameLocator(
'iframe[title="Payment"]');
await pay.getByLabel("Card name")
.fill("Alice Shah");
await pay.getByRole("button",
{ name: "Pay" }).click();
await expect(pay.getByText(
"Paid")).toBeVisible();
page.getByLabel("Card name")
→ not found (other document) Frames in Selenium
In Selenium, switch into the frame, found by an element locator, by index or by name. Inside it, find elements normally. Afterwards, switch to default content to return to the main page. Nested frames need one switch per level, and the expected condition frame to be available and switch to it waits for slow loading frames. Again, forgetting to switch back is the usual mistake.
d.switchTo().frame(
d.findElement(By.cssSelector(
"iframe[title=Payment]")));
d.findElement(By.id("n"))
.sendKeys("Alice Shah");
...
d.switchTo().defaultContent();
nested frames: switch one
level at a time
wait: frameToBeAvailable... Dialogs
Browser dialogs, alert, confirm and prompt, block the page. Playwright dismisses them automatically unless you handle them. To test one, register a dialog handler before the click, check its message, then accept or dismiss it. In Selenium, wait for the alert to be present, switch to it, read its text and accept or dismiss. Custom in page dialogs are just normal elements, often with the dialog role.
// Playwright: auto-dismissed
// unless you handle them
page.once("dialog", d => {
expect(d.message()).toContain(
"Delete");
d.accept();
});
await deleteButton.click();
// Selenium
d.switchTo().alert().accept(); Shadow DOM
Web components keep their internal elements in a shadow root, separate from the main DOM. Playwright locators look inside open shadow roots automatically, so you rarely notice. Selenium four gets the shadow root from the host element, then finds elements inside it with CSS. Closed shadow roots cannot be reached from outside, so test those components through what users and their public API can see.
- web components keep their DOM in a shadow root
- Playwright: locators pierce OPEN shadow roots automatically
- Selenium 4: element .getShadowRoot() .findElement(By.css...)
- closed shadow roots: test via the component's public API
Common mistakes
Mistakes. Starting to wait for a tab or download after the click, so the event is missed. Trying to drive the operating system's file picker. Checking only that a download exists, not what is in it. Forgetting to switch back to the main window or frame in Selenium. Locating iframe content from the main page. And sleeping while files save, instead of waiting for the download event.
- starting to wait AFTER the click (missed tab/download)
- driving the OS file picker
- only checking a download exists, not its content
- forgetting switchTo back
- locating iframe content from the main page
- sleeping for files to save
Say it like this
In the interview: for tabs, I wait for the context's page event together with the click in Playwright, or use window handles and switch to in Selenium. For iframes, a frame locator, or switch to frame and back to default content. Uploads use set input files or sending the path to the file input. Downloads: wait for the download event, save the file and check its content.
- "Tabs: wait for the context's
- page event together with the click; in Selenium, window
- handles and switchTo. iframes: frameLocator, or switchTo
- .frame and defaultContent.
- Uploads: setInputFiles or sendKeys a path. Downloads: wait for the event, save it
- and check the content."