Sunday, February 21, 2021

Edge Browser Open in C# Updated 


Install below nugget


using Microsoft.Edge.SeleniumTools;



  public void CreateDriver(string browser)

        {

            string baseUrl = GMR.GetSection().ServerUrl.ToString();


            if (browser?.Trim().ToUpper(CultureInfo.InvariantCulture) == "CHROME")

                _driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));

            else if (browser?.Trim().ToUpper(CultureInfo.InvariantCulture) == "IE")

            {

                InternetExplorerOptions options = new InternetExplorerOptions();

                options.RequireWindowFocus = true;

                //_driver = new InternetExplorerDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));

                _driver = new InternetExplorerDriver(InternetExplorerDriverService.CreateDefaultService(), new InternetExplorerOptions(), TimeSpan.FromMinutes(3));



            }

            else if (browser?.Trim().ToUpper(CultureInfo.InvariantCulture) == "FIREFOX")

            {

                _driver = new FirefoxDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));

            }

            


else if (browser?.Trim().ToUpper(CultureInfo.InvariantCulture) == "EDGE")

            {

                var options = new Microsoft.Edge.SeleniumTools.EdgeOptions();

                options.UseChromium = true;

                _driver = new Microsoft.Edge.SeleniumTools.EdgeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), options);


            }



            _driver.Navigate().GoToUrl(new Uri(baseUrl));

            currentDriver = new NgWebDriver(_driver);

        }



       

No comments:

Post a Comment