elements / new tab
Single UI element
New tab
Links and buttons that open new browsing contexts — useful for practicing window/tab-handle switching, asserting URLs, and returning to the parent tab.
Link with target="_blank"
#link-newtab- id
- link-newtab
- expected
- Opens a second tab; original tab stays on this page.
Code snippets
Python (Selenium)
orig_window = driver.current_window_handle
driver.find_element(By.ID, "link-newtab").click()
for handle in driver.window_handles:
if handle != orig_window:
driver.switch_to.window(handle)
break
assert "Inputs" in driver.title
driver.close()
driver.switch_to.window(orig_window)TypeScript (Playwright)
const [newPage] = await Promise.all([
context.waitForEvent('page'),
page.click('#link-newtab')
]);
await newPage.waitForLoadState();
expect(await newPage.title()).toContain('Inputs');
await newPage.close();Button-triggered window.open()
#btn-newwindow- id
- btn-newwindow
- expected
- Triggers
window.open('button.html', '_blank')via JavaScript rather than an anchor tag.
Code snippets
Python (Selenium)
orig = driver.current_window_handle
driver.find_element(By.ID, "btn-newwindow").click()
WebDriverWait(driver, 3).until(lambda d: len(d.window_handles) > 1)
new_handle = [h for h in driver.window_handles if h != orig][0]
driver.switch_to.window(new_handle)
assert "Buttons" in driver.titleTypeScript (Playwright)
const [newPage] = await Promise.all([
context.waitForEvent('page'),
page.click('#btn-newwindow')
]);
expect(await newPage.title()).toContain('Buttons');Same-tab navigation (control case)
#link-sametab- id
- link-sametab
- expected
- Navigates in the current tab — no new handle is created.
Code snippets
Python (Selenium)
driver.find_element(By.ID, "link-sametab").click()
assert "Homepage" in driver.title
assert len(driver.window_handles) == 1TypeScript (Playwright)
await page.click('#link-sametab');
expect(await page.title()).toContain('Homepage');