[英]How can I run my browser only one time
I want to start my browser only one time and then get unstatic information from there. 我只想启动浏览器一次,然后从那里获取非静态信息。 But my browser starts many times.
但是我的浏览器启动了很多次。 How can I start it only one time and close it, when my bool = false;
当我的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 ();
}
}
May this will work for you. 愿这对您有用。
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 ();
}
}
The reason behind this is while (GameIsRun)
code . 其背后的原因是
while (GameIsRun)
代码。 GameIsRun is always true that is why it goes to infinite loop. GameIsRun始终为真,这就是它进入无限循环的原因。
How you can overcome this issue : you have to make the value of GameIsRun false after launching the browser just like this : 如何解决此问题 :像这样启动浏览器后,必须将GameIsRun的值设置为false:
public static void CheckGame()
{
while (GameIsRun)
{
GetInformation(GetDriver());
GameIsRun = false;
}
}
Use this code , once the browser has launched , it would make GameIsRun
as false. 使用此代码,一旦启动浏览器,它将使
GameIsRun
为false。
Hope it'll help you to resolve your issue. 希望它能帮助您解决问题。
For that you can use singleton concept. 为此,您可以使用单例概念。
public sealed class Singleton
{
private static Singleton instance=null;
private Singleton()
{
}
public static Singleton Instance
{
get
{
if (instance==null)
{
instance = new Singleton();
}
return instance;
}
}
}
Hope this will help you. 希望这会帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.