Capture Browser Chrome URL & Tabs with Selenium and C
In this article
This guide shows you how to capture the browser's URL and open tabs (window handles) using Selenium WebDriver with C#. By the end, you'll have a small console app that connects to a running Chrome instance, lists all open tabs, and prints each tab's URL. You'll need Visual Studio (or any C# IDE), the Selenium.WebDriver NuGet package, and ChromeDriver matching your Chrome version.
What You'll Need
- Visual Studio 2022 or newer (Community edition is fine)
- .NET 6.0 or later (or .NET Framework 4.7.2+)
- Chrome browser installed
- ChromeDriver executable (download from chromedriver.chromium.org) – must match your Chrome major version
- Selenium.WebDriver NuGet package (install via Package Manager Console)
Step-by-Step
- Create a new console project – In Visual Studio, select "Create a new project" → "Console App (.NET Core)" or "Console App (.NET Framework)". Name it something like "CaptureTabs".
- Install Selenium.WebDriver – Open Tools → NuGet Package Manager → Package Manager Console, then run:
Install-Package Selenium.WebDriver. This adds the Selenium library to your project. - Download ChromeDriver – Check your Chrome version (chrome://settings/help). Download the matching ChromeDriver zip from the official site, extract it, and place
chromedriver.exein a known folder, e.g.,C:\WebDriver\. - Add required using directives – At the top of Program.cs, add:
using OpenQA.Selenium;andusing OpenQA.Selenium.Chrome;. - Configure Chrome options to attach to existing browser – Selenium can connect to a running Chrome instance if you start Chrome with remote debugging. First, close all Chrome windows. Then launch Chrome manually with the flag:
chrome.exe --remote-debugging-port=9222. This opens a debugging port. - Create ChromeDriver instance with options – In your code, create a
ChromeOptionsobject and setDebuggerAddresstolocalhost:9222. Then instantiateChromeDriverwith that options object and the path to chromedriver.exe:var options = new ChromeOptions(); options.DebuggerAddress = "localhost:9222"; var driver = new ChromeDriver(@"C:\WebDriver", options); - Get all window handles (tabs) – Use
driver.WindowHandlesto get a list of strings, each representing a tab. For example:var tabs = driver.WindowHandles.ToList(); - Loop through each tab and capture URL – For each handle, switch to that tab using
driver.SwitchTo().Window(handle), then readdriver.Url. Print the tab index and URL to the console. - Close the driver – After capturing, call
driver.Quit()to release resources. Note: this will close the Chrome instance you attached to if it was started with remote debugging.
Common Mistakes
- ChromeDriver version mismatch – If Chrome updates, ChromeDriver must be updated to the same major version. Otherwise you get a "session not created" error.
- Forgetting to start Chrome with remote debugging – If you don't pass the
--remote-debugging-portflag, Selenium cannot attach to the existing browser; it will try to start a new one. - Not closing all Chrome instances before starting – If Chrome is already running without the flag, the new instance with the flag may not open, or the port may be occupied.
- Using
driver.Urlwithout switching –driver.Urlonly returns the URL of the currently active tab. You must switch to each handle first. - Assuming
WindowHandlesorder is stable – The order is not guaranteed; always treat it as a set.
Tips
- To capture the page title as well, use
driver.Titleafter switching. - If you want to capture the URL of the active tab only, use
driver.Urldirectly without switching. - Use a
try-finallyblock to ensuredriver.Quit()is called even if an exception occurs. - To avoid hardcoding the ChromeDriver path, add it to your system PATH and use
new ChromeDriver(options)without the path. - If you need to capture tabs from a browser that's already open without restarting it, you can use the DevTools Protocol directly, but that's more complex.
Troubleshooting
- "Unable to connect to host 127.0.0.1 on port 7055" or similar – This usually means ChromeDriver can't find the debugging port. Ensure Chrome is running with the correct flag and that the port is not blocked by a firewall.
- "SessionNotCreatedException: This version of ChromeDriver only supports Chrome version XX" – Download the correct ChromeDriver version from the official site and replace the old one.
- Chrome opens a new window instead of attaching – You likely didn't pass the
DebuggerAddressoption, or you didn't start Chrome with the remote debugging flag. Double-check both. - No tabs are listed (empty list) – Make sure Chrome actually has at least one tab open. Also, if you attached to an existing Chrome, the handles are available immediately; if you started a new Chrome via Selenium, the initial handle is the only one.
- Access denied or permission errors when starting ChromeDriver – Run your IDE as administrator, or place chromedriver.exe in a folder with write permissions.
Frequently Asked Questions
Can I capture tabs from a Chrome window that was already open before I ran my script?
Yes, but you must have started that Chrome with the --remote-debugging-port=9222 flag. If it wasn't started that way, you cannot attach to it; you can only start a new Chrome instance.
Does this work with other browsers like Firefox or Edge?
The same concept works with Firefox (using geckodriver) and Edge (using msedgedriver), but the options class and driver names differ. For Edge, it's EdgeOptions and EdgeDriver.
How do I get the tab's title as well?
After switching to the tab with SwitchTo().Window(handle), use driver.Title to get the page title.
Will calling driver.Quit() close the Chrome browser I attached to?
Yes, it will close the Chrome instance that was started with remote debugging. If you don't want that, you can use driver.Close() to close only the current tab, but that may still exit the browser if it's the last tab.
Can I capture the URL of a specific tab without switching to it?
No, Selenium requires you to switch to the window handle to access its URL. There's no direct method to get a URL from a handle without switching.
What if my Chrome version is very old or very new?
ChromeDriver is released in sync with Chrome. Always download the version that matches your Chrome's major version. If you use Chrome Beta or Dev, you may need a matching ChromeDriver from the beta channel.