Tech Learner
Tech Learner

Reputation: 1317

How to open multiple web pages in chrome using Selenium Chrome Driver

I am using Selenium chrome driver to open URLs but when I try to open multiple sites, all are loading in a same tab one by one but I want to open each site in a separate chrome window.

IWebDriver driver;
ChromeDriverService chromeService = ChromeDriverService.CreateDefaultService();
chromeService.HideCommandPromptWindow = true;
ChromeOptions m_Options = new ChromeOptions();
m_Options.AddArgument("--disable-extensions");
driver = new ChromeDriver(chromeService, m_Options);
foreach (var url in urlList)
{
driver.Navigate().GoToUrl(url);
}

Can someone help me to identify the missing piece of code to open each URL in a new window.

Upvotes: 0

Views: 1224

Answers (1)

Prophet
Prophet

Reputation: 33351

In order to open each link in a new, separate tab you will have to open a new tab and switch to that tab before opening the next URL link.
Something like this:

foreach (var url in urlList)
{
    ((IJavaScriptExecutor)driver).ExecuteScript("window.open();");
    driver.SwitchTo().Window(driver.WindowHandles.Last());
    driver.Navigate().GoToUrl(url);
}

Be aware that if you are going to open too many windows without closing them this may cause memory lack so that it may case serious problems.

Upvotes: 1

Related Questions