c# - 自动打开google chrome,点击按钮,然后关闭chrome C# WinForms

标签 c# .net windows forms html

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

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

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

感谢您的帮助

最佳答案

实现此目的的最佳方法是创建一个小型 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 和密码变量。

关于c# - 自动打开google chrome,点击按钮,然后关闭chrome C# WinForms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29285261/

相关文章:

c# - .Net 垃圾回收标记阶段和巨大的链表

linux - UDP 连接被拒绝

java - 如何在命令提示符中创建特定命令的快捷方式?

windows - COMAdmin.COMAdminCatalog - 如何获取 DCOM Config 文件夹内容?

c# - 我的日志管理器的路径共享违规

c# - 自定义身份验证和 ASP.NET MVC

C# 字符串枚举作为 NHibernate 鉴别器

c# - 无需使用库即可解析命令行的 RegEx 函数

asp.net - Swagger UI - 类型错误 : Failed to fetch - on endpoint request (ASPNET Core API)

c# - 在 C# 中删除 GridViewRow 的顶部边框