【问题标题】:System.Windows.Forms.SendKeys.SendInput >> Access denied at Task SchedulerSystem.Windows.Forms.SendKeys.SendInput >> 任务计划程序拒绝访问
【发布时间】:2014-03-16 11:39:40
【问题描述】:

我遇到了一个模糊问题,这里是以下设置。

我有一个小型 C# 应用程序,用于每天使用 Windows 2012R2 服务器上的任务计划程序登录到某个网站。

这是 HTML 登录代码段:

<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,它可以打开一个带有用户和密码字段的网站。应用程序检查属性并将输入发送到登录。 代码片段如下:

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 用户没有登录,所以它无法输入字符串。

所以这是我的问题,我该如何解决这个问题?我愿意接受每一个建议/进一步的解释!

【问题讨论】:

    标签: c# scheduled-tasks access-denied login-script autologin


    【解决方案1】:

    您可以通过使用登录表单按钮的单击事件轻松避免 SendKey 方法。 使用 ID 为“提交”的按钮遵循您的编码风格:

    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");
    

    【讨论】:

    • 感谢您的帮助!我试过(没有 if),但得到了一个 NullReferenceException。看起来他没有找到按钮。我还在我的帖子中添加了 html 登录片段
    【解决方案2】:

    这个有效!

    tagsColl = webBrowser1.Document.GetElementsByTagName("button");
            foreach (HtmlElement curElement in tagsColl)
            {
                if (curElement.GetAttribute("submit").Equals(""))
                {
                    curElement.InvokeMember("click");
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-14
      • 1970-01-01
      • 2019-11-11
      • 1970-01-01
      相关资源
      最近更新 更多