自动打开谷歌浏览器,单击按钮,然后关闭chrome C#WinForms

时间:2015-03-26 17:39:48

标签: c# .net windows forms webpage

我有一个任务,因为我已经写过主题。我需要用网页打开谷歌浏览器,然后我想点击按钮并关闭铬。我希望它在Windows窗体.net 4.0中并用C#编写。我试图做那样的事情:

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

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

感谢您的帮助

3 个答案:

答案 0 :(得分:3)

最好的方法是创建一个小的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和密码变量。

答案 1 :(得分: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您自己或您必须通过win表单中的Web浏览器控件打开您的网页并通过它捕获事件。

答案 2 :(得分:0)

我不知道您的全部要求,但我认为您可能希望查看一些自动化工具,例如selenium Selenium

您可以在此处查看https://code.google.com/p/selenium/wiki/GettingStarted

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

希望这有帮助。