'How to open multiple web pages using Selenium Driver in .Net?

I am using Selenium IWebDriver to open multiple web sites in chrome. I have add button in my .Net application's UI and when I enter website address and click on Add button it should open in the Chrome browser.

For me, First web site opens in a tab and works properly but when I enter second address and click on Add button new tab is opening but website is not loading. Below is the code I have used in my add button.

ChromeDriverService chromeService = ChromeDriverService.CreateDefaultService();
ChromeOptions options = new ChromeOptions();
IWebDriver webDriver = new ChromeDriver(chromeService, options);
webDriver.Navigate().GoToUrl(websiteAddress);

Can someone help me to identify the issue in my code?



Solution 1:[1]

Create a function for driver that returns the chrome driver.

public ChromeDriver ContextDriver(bool Headless)
    {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.AddArguments("--ignore-certificate-errors");
        chromeOptions.AddArguments("--allow-running-insecure-content");

        if (Headless)
        {
            chromeOptions.AddArguments("--window-size=1920,1080");
            chromeOptions.AddArguments("headless");
        }
        ChromeDriver ContextDriver = new ChromeDriver("PATH_TO_CHROME_DRIVER", chromeOptions);
        ContextDriver.Manage().Window.Maximize();
        ContextDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
        return ContextDriver;
    }

Now you can use this on multiple websites all at ones by creating multiple contexts.

ChromeDriver ContextDriver = ContextDriver(false);
ContextDriver.Navigate().GoToUrl(url);

or if you have the urls in a list you can use a foreach loop over them.

foreach(var url in urlList)
{
    ChromeDriver ContextDriver = ContextDriver(false);
    ContextDriver.Navigate().GoToUrl(url);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 KingKong BigBong