我有一个任务,正如我在主题中写的那样。我需要使用网页打开 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/