首页 > 解决方案 > 如何只运行一次浏览器

问题描述

我只想启动一次浏览器,然后从那里获取非静态信息。但是我的浏览器启动了很多次。当我的 bool = false 时,我怎样才能只启动一次并关闭它;

class Program
    {
        public static bool GameIsRun = true;
        static void Main(string[] args)
        {
            CheckGame();
        }

        public static ChromeDriver GetDriver()
        {
            return new ChromeDriver();
        }

        public static void CheckGame()
        {
            while (GameIsRun)
            {
                GetInformation(GetDriver());
            }
        }

        static void GetInformation(ChromeDriver driver)
        {
            driver.Navigate().GoToUrl("myURL");
            do
            {
               //loop for doing something on this page, I don't want to start my browser many times.  
            }
            while ();
        }
    }

标签: c#loopsseleniumwebdriverinstance

解决方案


愿这对你有用。

class Program
    {
        public static bool GameIsRun = true;
        public static IWebDriver driver = null;
        static void Main(string[] args)
        {
            CheckGame();
        }

        public static ChromeDriver GetDriver()
        {
            if(driver == null){
                 driver = new ChromeDriver();
            }
            return driver;
        }

        public static void CheckGame()
        {
            while (GameIsRun)
            {
                GetInformation(GetDriver());
            }
        }

        static void GetInformation(ChromeDriver driver)
        {
            driver.Navigate().GoToUrl("myURL");
            do
            {
               //loop for doing something on this page, I don't want to start my browser many times.  
            }
            while ();
        }
    }

推荐阅读