命令行打开Chrome,等到它关闭 [英] Open Chrome from command line and wait till it's closed

查看:1262
本文介绍了命令行打开Chrome,等到它关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的代码来遍历的浏览器可执行文件的路径列表,并开始他们每个人的:

 的foreach(在浏览器中浏览器变种)
{
变种PROC =新工艺();
proc.StartInfo.FileName = browser.ExecutablePath;
proc.StartInfo.Arguments =htt​​p://google.com;
proc.Start();
proc.WaitForExit();
Console.WriteLine(proc.ExitCode.ToString());
proc.Close();
}



什么是应该做的是:它应该打开浏览器窗口的谷歌.COM 的加载,直到窗口关闭停止应用程序。它工作正常的IE和火狐,但无法与Chrome浏览器。



有关Chrome浏览器 PROC 退出状态刚刚启动浏览器,当窗口仍然是积极和明显的后。



我使用一些铬命令行开关尝试,其中 - 新窗口 - 单进程但没有成功。

问题是,我怎么能迫使谷歌Chrome浏览器在它在启动过程中运行,因此将有可能要等到窗口关闭?



更新



只是为了澄清这个问题:




  • 我知道为什么它不工作 - 这是因为Chrome使用了不同的东西多进程,如不同的标签,插件等。

  • 我试图找到正确的进程看好进程树,但一无所获。

  • 我不能只是采取镀铬创造了最新的工艺,因为它可能是过程中产生的pluging页面需要,而不是网页本身产生的。


解决方案

如果你想打开/关闭整个浏览器窗口:



默认浏览器是不断在后台运行,因为它的默认设置。将此选项更改为未选中:结果
设置>显示高级>系统> 继续运行后台应用时,谷歌Chrome关闭



所以,你必须确保Chrome关闭你又被你的代码运行之前, 。 ,这对我的作品






如果你想打开/关闭相关的标签只有:



浏览器有一个母亲的过程和一些子进程。
的问题是,如果你使用你的代码运行铬你要创建的母亲进程的新实例,但在现有的不会让你做到这一点。她会立刻杀了你的新工艺,创造了她自己的孩子来代替。这就是它是如何工作的?



因此​​,所有你需要的是弄清楚如何运行另一个母亲的镀铬工艺并防止以前从杀死她的拥抱; p



我想出此解决方案:结果
用这个参数运行新的镀铬工艺 - 用户-data-DIR =%temp%/ random_name。这意味着,您正在使用新的用户配置文件打开浏览器。



优点:




  • 它的工作原理

  • <李>铬在新窗口中打开
  • Chrome浏览器时,所有相关的分页关闭结束



缺点:




  • 默认设置(无书签等),但你可以从默认的用户配置文件目录复制它们



所以,也许你应该看看做工在这个方向.​​.....


I'm using following code to iterate over a list of browsers executable paths and start each of them:

foreach (var browser in browsers)
{
    var proc = new Process();
    proc.StartInfo.FileName = browser.ExecutablePath;
    proc.StartInfo.Arguments = "http://google.com";
    proc.Start();
    proc.WaitForExit();
    Console.WriteLine(proc.ExitCode.ToString());
    proc.Close();
}

What is should do is: it should open browser window with google.com loaded and stop the application until the window is closed. And it works fine for both IE and Firefox, but fails with Chrome.

For Chrome proc is in Exit state just after launching the browser, when the window is still active and visible.

I tried using some of chromium command line switches, including --new-window and --single-process but with no success.

Question is, how can I force Google Chrome to run in the process it is started in, so it would be possible to wait until window is closed?

Update

Just to clarify the question:

  • I know why it does not work - it's because Chrome uses multiple processes for different things, like different tabs, plug-ins, etc.
  • I tried to find the correct process looking on process tree, but found nothing.
  • I can't just take the latest process created by chrome, because it may be the process created for a pluging the page requires, not the page itself.

解决方案

If you want to open/close entire Chrome window:

Chrome by default is constantly running in background because of its default settings. Change this option to unchecked:
Settings > Show advanced > System > 'Continue running background apps when Google Chrome is closed'

So you have to make sure Chrome is closed before you run it again by your code. That works for me.


If you want to open/close related tabs only:

Chrome have one 'mother process' and some child processes. The problem is that if you run chrome using your code you are trying to create new instance of 'mother process' but the existing one won't let you do it. She'll instantly kill your new process and create her own child instead. That's how it works...

So, all you need is figure out how to run another 'chrome mother process' and prevent the previous hug from killing her ;P

I figure out this solution:
Run new chrome process with this parameter --user-data-dir="%temp%/random_name". This means that you are opening chrome with new user profile.

Advantages:

  • It works
  • Chrome is opening in new window
  • Chrome is closing when all related tabs are closed

Disadvantages:

  • Default settings (no bookmarks, etc) but you can copy them from default user profile directory

So, maybe you should look for sth in this direction...

这篇关于命令行打开Chrome,等到它关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆