Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何只运行一次浏览器_C#_Loops_Selenium_Webdriver_Instance - Fatal编程技术网

C# 如何只运行一次浏览器

C# 如何只运行一次浏览器,c#,loops,selenium,webdriver,instance,C#,Loops,Selenium,Webdriver,Instance,我只想启动我的浏览器一次,然后从那里获取非静态信息。但是我的浏览器启动了很多次。当我的bool=false时,如何只启动一次并关闭它 class Program { public static bool GameIsRun = true; static void Main(string[] args) { CheckGame(); } public static ChromeDriv

我只想启动我的浏览器一次,然后从那里获取非静态信息。但是我的浏览器启动了很多次。当我的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 ();
        }
    }

愿这对你有用

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 ();
        }
    }

愿这对你有用

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 ();
        }
    }

这背后的原因是
while(GameIsRun)
codeGameIsRun总是正确的,这就是它进入无限循环的原因

如何克服此问题:启动浏览器后,必须将GameIsRun的值设置为false,如下所示:

public static void CheckGame()
            {
                while (GameIsRun)
                {
                    GetInformation(GetDriver());
                    GameIsRun = false;
                }
            }
使用此代码,一旦浏览器启动,它将使
GameIsRun
为false


希望它能帮助你解决你的问题

这背后的原因是
while(GameIsRun)
codeGameIsRun总是正确的,这就是它进入无限循环的原因

如何克服此问题:启动浏览器后,必须将GameIsRun的值设置为false,如下所示:

public static void CheckGame()
            {
                while (GameIsRun)
                {
                    GetInformation(GetDriver());
                    GameIsRun = false;
                }
            }
使用此代码,一旦浏览器启动,它将使
GameIsRun
为false


希望它能帮助你解决你的问题

为此,您可以使用单例概念

public sealed class Singleton
{
  private static Singleton instance=null;

  private Singleton()
  {
  }

  public static Singleton Instance
  {
    get
    {
        if (instance==null)
        {
            instance = new Singleton();
        }
        return instance;
    }
   }
 }

希望这将对您有所帮助。

因此您可以使用单例概念

public sealed class Singleton
{
  private static Singleton instance=null;

  private Singleton()
  {
  }

  public static Singleton Instance
  {
    get
    {
        if (instance==null)
        {
            instance = new Singleton();
        }
        return instance;
    }
   }
 }


希望这能对你有所帮助。

你听说过“如果还有”吗?是的,当然。一次启动浏览器对我有什么帮助?你听说过“如果还有”吗?是的,当然。它如何帮助我启动浏览器一次?好的,谢谢你花时间在我身上。我需要一些时间来检查。@Murthi:你试过自己的答案吗?您提供的代码具有流。问题是它不会每次都打开新窗口,但由于while(GameIsRun)循环,它每次都会重新加载相同的URL。是的,我修复了它。非常感谢你抽出时间陪我!严重性代码说明项目文件行抑制状态错误CS0103当前上下文中不存在名称“driver”testo C:\Users\The Vee\source\repos\testo\testo\Program.cs 31 ActiveFixed!谢谢,对不起!好的,谢谢你花时间陪我。我需要一些时间来检查。@Murthi:你试过自己的答案吗?您提供的代码具有流。问题是它不会每次都打开新窗口,但由于while(GameIsRun)循环,它每次都会重新加载相同的URL。是的,我修复了它。非常感谢你抽出时间陪我!严重性代码说明项目文件行抑制状态错误CS0103当前上下文中不存在名称“driver”testo C:\Users\The Vee\source\repos\testo\testo\Program.cs 31 ActiveFixed!谢谢,对不起@TheVee:Murthi解决方案在我看来是不对的,我试过他的代码。他的代码对你有用吗?@TheVee:他的代码中发生了什么,每次都会重新加载相同的URL。因为时间(GameIsRun)总是正确的。请验证此行为。我使用此选项,浏览器只启动一次。@Vee:我使用此选项表示我的答案?如果是这样,你应该接受这个答案,而不是其他人的。请注意,future redaer更喜欢接受答案。@Vee:Murthi解决方案在我看来不正确,我尝试了他的代码。他的代码对你有用吗?@TheVee:他的代码中发生了什么,每次都会重新加载相同的URL。因为时间(GameIsRun)总是正确的。请验证此行为。我使用此选项,浏览器只启动一次。@Vee:我使用此选项表示我的答案?如果是这样,你应该接受这个答案,而不是其他人的。请注意,未来的redaer更倾向于接受答案。