问题
I got a fuzzy problem, here is the following setup.
I got a small C# application to login to a certain website on a daily basis with a task scheduler on a Windows 2012R2 Server.
Here is the HTML login 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>
The application has a Form.WebBrowser which opens a website with a user and password field. The application checks for a attribute and send enter to login. Here the code 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}");
}
The application works totally fine when Im running it manually.
About the task scheduler setup. I tried a lot of different setups, have a look here:
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
When the task scheduler running the job I always got the following exception.
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 ---
I guess it is because the Windows User is not logged in probably so it cant input the strings.
So here is my question, how can I come around this problem? Im open for every suggestion/further explanation!
回答1:
You can easily avoid the SendKey method by using the click event of the button of the login form. To follow your coding style with a button with id "submit":
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");
回答2:
This one works!
tagsColl = webBrowser1.Document.GetElementsByTagName("button");
foreach (HtmlElement curElement in tagsColl)
{
if (curElement.GetAttribute("submit").Equals(""))
{
curElement.InvokeMember("click");
}
}
来源:https://stackoverflow.com/questions/22436352/system-windows-forms-sendkeys-sendinput-access-denied-at-task-scheduler