> For the complete documentation index, see [llms.txt](https://docs.zerowork.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.zerowork.io/using-zerowork/using-building-blocks/switch-or-close-tab.md).

# Switch or Close Tab

<figure><img src="https://3220183989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fg9LcR8XM7TGOzOCDGsCS%2Fuploads%2FzpSCqbx40bYbrXveiIvy%2Fswitch_or_close_tab_block.png?alt=media&amp;token=cc3b680a-6d0e-4aae-9861-6672431c3d7d" alt="" width="375"><figcaption></figcaption></figure>

The **Switch or Close Tab** block changes which browser tab your TaskBot works in, or closes a tab it no longer needs.

{% hint style="info" %}
Your TaskBot works in one browser tab at a time. The browser tab it's currently working in is referred to as the **active page**. Web Interaction blocks like [Click Web Element](/using-zerowork/using-building-blocks/click-web-element.md) and [Save Web Element](/using-zerowork/using-building-blocks/save-web-element.md) take their actions on this active page. Use this block when you need to change your TaskBot's active page, or when you need to close browser tabs that have piled up during your TaskBot's run.
{% endhint %}

## How To Use It

<figure><img src="https://3220183989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fg9LcR8XM7TGOzOCDGsCS%2Fuploads%2FF9Sm3SYudyN8iBCX4BJ9%2Fswitch_or_close_tab_settings.png?alt=media&amp;token=7c823e81-2f78-47ef-be82-0d900239d05d" alt=""><figcaption></figcaption></figure>

### Action

* **Switch to tab** makes another already opened tab the active page.
* **Close tab** closes a tab.

### Target Tab

How the tab to act on is chosen. Where possible, prefer **Tab URL matching** because it identifies a tab by what's in its URL rather than by where it sits in the browser, so it keeps working if tabs open in a different order than you expected or get rearranged mid-run.

{% hint style="warning" %}
**Sticky Profiles**<br>

When several TaskBots share a sticky profile they run in the same browser instance and see each other's browser tabs. Tab positions can shift as the other TaskBots open and close tabs, so **Latest tab**, **Previous tab**, **Next tab**, and **Tab number** can potentially reference a tab that a different TaskBot is currently using. Additionally, the **Close tab** action of this block can close a tab that another TaskBot using the same sticky profile is working in, ending the latter's in-progress run. Use **Tab URL matching** within this block whenever your TaskBot uses a sticky profile shared with other TaskBots.
{% endhint %}

* **Latest tab** is the most recently opened tab.
* **Previous tab** / **Next tab** is the tab immediately before or after the active one. These options use the order in which your tabs were opened to determine which browser tab to switch to or close.
* **Tab URL matching** is the first tab whose address matches the text or regex you provide. This field takes a full URL, part of one, or a regular expression. Text is matched against any part of a tab's address, and against the final address after any redirects. To use a regular expression, wrap it in forward slashes (for example, `/example\.com\/orders\/\d+/i`, where `i` makes the match case-insensitive).

{% hint style="warning" %}
**Regular Expressions**\
\
Anything that isn't a valid regular expression in the **Tab URL matching** field is matched as plain text instead, and no error is reported. A pattern missing its closing slash, such as `/help`, is searched for literally. A typo here can change what you match rather than failing, so check anything complicated with a regex validator such as [regex101](https://regex101.com/) before using it.
{% endhint %}

{% hint style="success" %}
The **Tab URL matching** field accepts dynamic inputs, such as variable and table references, as well as code expressions. Note [spintax](/using-zerowork/using-building-blocks/dynamic-inputs/spintax.md) is not supported in this field. See [Dynamic Inputs](https://docs.zerowork.io/using-zerowork/using-building-blocks/dynamic-inputs) for more information.
{% endhint %}

* **Tab number** is counted in the order tabs were opened (each tab's creation time), starting at **1**. This field does **not** support [Dynamic Inputs](/using-zerowork/using-building-blocks/dynamic-inputs.md).

### Min Sec & Max Sec

A pause after this block's action completes, before the next block runs. The wait is randomized between the two values, and setting both to the same number makes it fixed.\
\
This block defaults to **0**, meaning no pause before the next block executes.

{% hint style="info" %}
See the [Delay](/using-zerowork/using-building-blocks/delay.md) block for how these two fields behave, including decimals, references, and what happens when a field is left empty. See [Delay Times between the Building Blocks](/using-zerowork/using-building-blocks/building-block-options/delay-times-between-the-building-blocks.md) for how block-level delays fit into a TaskBot's run.
{% endhint %}

## Things To Know

* **When the tab can't be found, your TaskBot keeps going.** Nearly every potential problem here is a warning rather than an error: no tabs open, only one tab when you asked to switch, a tab number that doesn't exist, no URL match, no previous or next tab to move to. Each of these logs a warning and the active page doesn't change. If later blocks act on the wrong page, this is the first thing to check in your TaskBot's [Run Reports](/using-zerowork/using-run-reports.md).
* **Closing the last tab quits the browser.** With only one tab open, closing it ends the browser session and no further web interactions are possible. **Stay on page after run** in your TaskBot's settings does not prevent this; an explicit Close Tab action in this block takes priority. You can start a new browser with an [Open Link](/using-zerowork/using-building-blocks/open-link.md) or [Write JavaScript](/using-zerowork/using-building-blocks/write-javascript.md#browser-and-context) block.
* **Closing the active page's tab moves you to a different browser tab.** Your TaskBot switches to the next tab in order of creation time, or to the previous one if you closed the last tab. Your TaskBot's [Run Reports](/using-zerowork/using-run-reports.md) records which tab became active.
* **Tab numbers follow creation order, not screen position.** Tab 1 is the first tab your TaskBot opened, which is normally also the leftmost. If tabs are manually dragged into a different order during the run, tab numbers no longer match what you might visibly see in the browser while your TaskBot is running. Numbering starts at 1; a number below 1, or text that isn't a number, fails the run rather than warning.
* **Closing a tab re-numbers the ones after it.** Tab numbers are positions in the browser's list of open tabs, calculated each time the block runs. Close tab 1 and the old tab 2 becomes tab 1. If you're closing several tabs by number, work from the highest down, or match on URL instead.
* **Extension pages don't count.** Sticky profiles support browser extensions, so when using sticky profiles, browser tabs that represent a browser extension are excluded from your TaskBot's browser tab numbering system.

## Block Behavior

* **When switching to a tab that exists** the block makes it the active page and records the switch in the run log.
* **When the target tab can't be found** the block logs a warning, changes nothing, and the run continues.
* **When closing a tab that isn't active** the tab closes and your TaskBot's active page stays where it is.
* **When closing the active tab** your TaskBot moves to the next tab in order first (or to the previous one if you closed the last tab), and then closes the tab you specified within this block. For example, with three tabs open and the middle one active, closing it moves your TaskBot to the last tab; whereas closing the last tab instead moves it to the middle one. Your TaskBot [Run Reports](/using-zerowork/using-run-reports.md) names the tab it moved to.
* **When closing the only open tab** the browser exits and a warning is logged, but the run itself continues. The next block that needs a browser then fails, stopping the run and reporting it as failed unless an [Open Link](/using-zerowork/using-building-blocks/open-link.md) or [Write JavaScript](/using-zerowork/using-building-blocks/write-javascript.md#browser-and-context) block has relaunched one first.
