我遇到了模糊问题,这是以下设置。
我有一个小型的C#应用程序,每天使用Windows 2012R2服务器上的任务调度程序登录某个网站。
以下是 HTML登录snipet:
<div class="control-group row-space-1">
<input class="decorative-input" id="signin_email" name="email" placeholder="Email Address" type="email" />
</div>
<div class="control-group row-space-2">
<input class="decorative-input" id="signin_password" name="password" placeholder="Password" type="password" />
</div>
<div class="clearfix row-space-2">
<label for="remember_me2" class='checkbox remember-me'>
<input type="checkbox" name="remember_me" id="remember_me2" value="true" class="remember_me"/>
Remember me
</label>
<a href="/forgot_password" class="forgot-password">Forgot password?</a>
</div>
<button type="submit" class="btn btn-block btn-primary large btn-large padded-btn-block">
Log In
</button>
该应用程序有一个Form.WebBrowser,它打开一个带有用户和密码字段的网站。应用程序检查属性并将enter发送到login。 此处代码为snipet:
HtmlElementCollection tagsColl=webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement currentTag in tagsColl)
{
if (currentTag.Name.Equals("email"))
currentTag.SetAttribute("value", username);
if (currentTag.Name.Equals("password"))
currentTag.SetAttribute("value", password);
currentTag.Focus();
SendKeys.Send("{ENTER}");
}
当我手动运行它时,应用程序完全正常。
关于任务计划程序设置。我尝试了很多不同的设置,请看一下:
Account:
local\Administrator
=> Error: Access denied
[x] Run only when user is logged in
=> Error: Access denied
[x] Run weather user is logged in or not
=> The Form with the WebBrowser doesent appear, I can see the task running in the task manager but nothing happens
[x] Run with highest privilegs
=> No effect
Configure for: Windows Server 2012 R2
当运行作业的任务调度程序时,我总是得到以下异常。
Error: Access denied
************** Exception Text **************
System.ComponentModel.Win32Exception (0x80004005): Access is denied
at System.Windows.Forms.SendKeys.SendInput(Byte[] oldKeyboardState, Queue previousEvents)
at System.Windows.Forms.SendKeys.Send(String keys, Control control, Boolean wait)
at System.Windows.Forms.SendKeys.Send(String keys)
at airbnblogin.Form1.autofill(String username, String password)
at airbnblogin.Form1.<main>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
我想这是因为Windows用户可能没有登录,因此无法输入字符串。
所以这是我的问题,我怎么能解决这个问题?我愿意接受每一个建议/进一步的解释!
答案 0 :(得分:1)
您可以使用登录表单按钮的click事件轻松避免使用SendKey方法。 要使用ID为#34的按钮关注您的编码风格,请提交&#34;:
HtmlElementCollection tagsColl=webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement currentTag in tagsColl)
{
if (currentTag.Name.Equals("email"))
currentTag.SetAttribute("value", username);
if (currentTag.Name.Equals("password"))
currentTag.SetAttribute("value", password);
}
HtmlElement button = webBrowser1.Document.GetElementById("submit");
if (button) button.InvokeMember("click");
答案 1 :(得分:0)
这个有效!
tagsColl = webBrowser1.Document.GetElementsByTagName("button");
foreach (HtmlElement curElement in tagsColl)
{
if (curElement.GetAttribute("submit").Equals(""))
{
curElement.InvokeMember("click");
}
}