0

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.

Tech Learner
  • 1,227
  • 6
  • 24
  • 59
  • For each url to open in a new window, you need new driver instance for each url. Try keeping the `driver = new ChromeDriver(chromeService, m_Options);` inside the loop – Anand Gautam Feb 02 '22 at 06:58
  • This might help if multiple tabs (not windows) works for you https://stackoverflow.com/a/42417215/5226491. You'll have to switch to each window for interacting with it, but will work with a single driver instance. – Max Daroshchanka Feb 02 '22 at 07:14
  • @AnandGautam - I tired creating new driver instance for each URL, first URL is loading fine but for second URL, tab is opening but web page is not loading. Even I tried with different URL and it is not working. – Tech Learner Feb 02 '22 at 07:28

1 Answers1

1

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.

Prophet
  • 32,350
  • 22
  • 54
  • 79