【问题标题】:Automatically open google chrome, click button, and close chrome C# WinForms自动打开google chrome,点击按钮,关闭chrome C# WinForms
【发布时间】:2015-03-26 17:39:47
【问题描述】:

我有一个任务,正如我在主题中所写的那样。我需要用网页打开 google chrome,然后我想单击按钮并关闭 chrome。我希望它在 Windows Form .net 4.0 中并用 C# 编写。我尝试过这样做:

System.Diagnostics.Process.Start(@"chrome.exe", "webpage");

这运行良好,但我不知道如何强制应用程序单击此页面上的按钮并关闭 chrome。

感谢您的帮助

【问题讨论】:

  • 我认为您可以使用 GreaseMonkey 之类的方式更轻松地做到这一点
  • 您想通过 Windows 窗体上的按钮关闭显示您网页的浏览器吗?我说的对吗?
  • 使用 HttpClient 发送 Chrome 会发送的请求不是更好吗?使用 Fiddler 之类的东西来获取确切的请求....
  • Syed -> 它如何关闭并不重要 - 它可以像你说的那样,或者无论如何,Dan -> 是的,如果你认为这可能会更好,请帮助我如何开始.您是否获得例如教程或任何材料的链接?
  • @Roland - 打开页面并单击按钮似乎不是任何主要的功能需求。没有人会看页面,谁在乎它如何呈现?话虽如此,如果您无法获得一个简单的 html 表格来使用它,那么您的 html 就会出现严重问题。内置的WebBrowser使用IE渲染引擎,IE能做的,它都能做。

标签: c# .net windows forms webpage


【解决方案1】:

您最好的方法是创建一个小的 HTML 文件,该文件将使用 Javascript 为您执行此操作。这样您就可以在 chrome 中专门打开该文件,它会为您完成。如果您需要特定代码,请告诉我。几个月前我不得不这样做。

    public static void TryWebBrowser()
    {
        bool _jsLoaded = false;
        string _directory = AppDomain.CurrentDomain.BaseDirectory;

        System.Diagnostics.ProcessStartInfo _sinfo = new    
        System.Diagnostics.ProcessStartInfo(_directory + "loginFile.html");
        System.Diagnostics.Process.Start(_sinfo);

        while (_jsLoaded == false) 
        {
            System.Diagnostics.Process[] _runningProcesses = 
            System.Diagnostics.Process.GetProcesses();
            foreach (System.Diagnostics.Process _p in _runningProcesses)
            {
                if (_p.MainWindowTitle.Contains("Jaspersoft"))
                {
                    _jsLoaded = true;
                    break;
                }
            }

        }

    }

有点像这样。

<html>
  <head>
    <title>Launching Jaspersoft</title>
    <script src="other.js" language="javascript" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">
        function submitForm() {
            document.getElementById("j_username").value = uid
            document.getElementById("j_password").value = pwd
            document.loginForm.action = action
            document.loginForm.submit()
        }


    </script>
  </head>
  <body onload="submitForm()">
    <form name="loginForm" method="post" action="">
      <input type="hidden" value="" name="j_username" id="j_username" />
      <input type="hidden" value="" name="j_password" id="j_password" />

    </form>

  </body>
</html>

other.js 只包含动态写入的 uid 和密码变量。

【讨论】:

    【解决方案2】:

    用途:

    using System.Diagnostics;
    

    在按钮点击事件中写入:

    Process[] AllProcess = Process.GetProcesses();
                foreach (var process in AllProcess)
                {
                    if (process.MainWindowTitle != "")
                    {
                        string s = process.ProcessName.ToLower();
                        if (s == "chrome" )
                            process.Kill();
                    }
                }
    

    改编自: http://www.codeproject.com/Questions/794195/Close-the-all-browser-windows-IE-google-chrome-fir
    编辑:
    上面的代码负责关闭部分。但是,对于单击按钮
    如果 Chrome 会以简单的方式将按钮单击等方法公开给外部应用程序,那将是令人惊讶的。
    您可能需要编写一个扩展程序,请参阅:https://developer.chrome.com/extensions 您自己的,或者您必须通过 web 浏览器控件在 win 表单中打开您的网页并通过它捕获事件。

    【讨论】:

    • 好的,现在我知道 chrome 的打开和关闭方式了 - 但我如何才能点击页面上的正确按钮?
    • 我了解您需要做什么,但如果 Chrome 会以简单的方式向外部应用程序公开按钮单击等方法,我会感到惊讶。您可能需要自己编写扩展程序 see :code.google.com/chrome/extensions/index.html,或者您必须通过 web 浏览器控件在 win 表单中打开您的网页并通过它捕获事件。
    • 使浏览器窗口成为活动窗口。然后使用 SendKeys.SendWait("{ENTER}");如果默认按钮是请求的按钮。或者添加一些“{TAB}”以访问请求的按钮
    【解决方案3】:

    我不知道你的全部要求,但我想你可能想看看一些自动化工具,比如 selenium Selenium

    您可以在这里查看入门指南https://code.google.com/p/selenium/wiki/GettingStarted

    它是用 Java 编写的,但它们也有 .net api: http://selenium.googlecode.com/svn/trunk/docs/api/dotnet/index.html

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-22
      • 2016-09-22
      • 1970-01-01
      • 2021-11-25
      • 2014-01-01
      • 1970-01-01
      相关资源
      最近更新 更多