none
SendKeys.Send sends same keystrokes multiple times RRS feed

  • Question

  • Trying to send user name and password to web browser control in Visual c# form. I am able to send keys to the web page on load using:

    SendKeys

     

    .Send("userid");

    SendKeys.Send("{TAB}");

     

    SendKeys.Send("password");

     

    SendKeys.Send("{TAB}");

     

    SendKeys.Send("{ENTER}");

    The problem is, it continually sends the keys, even after logged into the web page. How do I stop this?

    Tuesday, November 9, 2010 9:13 PM

Answers

  • Hi mikey814,

     

    Welcome to MSDN Forums!

     

    Where did you place these codes? Can you clarify your question? You also can tell us the method name you deal with which event related to these code lines.

    And I suggest you to add a breakpoint then use single-debug to watch the code executed path, then you can know why those codes would be execute not only one time.

     

    And I also agree with Ali Hamdar to use a flag to judge when the code need to be execute once.

     

    We also use invoke method and HtmlElement to input the information into the TextBox in html in WebBrowser.

    Such as, 

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
     this.button1.Text = "completed";
     if (this.webBrowser1.Document.Url.AbsoluteUri == "http://www.bing.com/")
     {
      HtmlDocument htmldoc = this.webBrowser1.Document;
      HtmlElement htmlelement = htmldoc.GetElementById("sb_form_q");
      htmlelement.SetAttribute("Value", "abcdef");
      htmldoc.Forms["sb_form"].InvokeMember("submit");
      MessageBox.Show("submit");
     }
     MessageBox.Show("DocumentCompleted");
    }
    
    

    If there’s anything unclear, please feel free to let me know.

     

    Best wishes,

    Mike

    -------------------------------------------------------------------

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to the other community members reading the thread.

    *****************************************************

    [All-In-One Code Framework]

    Sample world! You will get more from this world!

    Welcome to the new world!

    Sunday, November 14, 2010 12:15 PM
    Moderator

All replies

  • It seems that the event you placed the code on is being triggered multiple times - I advice you to use a flag that the sendkeys functions are called to prevent calling them again.
    Ali Hamdar (alihamdar.com - www.ids.com.lb)
    Wednesday, November 10, 2010 12:59 AM
  • Hi mikey814,

     

    Welcome to MSDN Forums!

     

    Where did you place these codes? Can you clarify your question? You also can tell us the method name you deal with which event related to these code lines.

    And I suggest you to add a breakpoint then use single-debug to watch the code executed path, then you can know why those codes would be execute not only one time.

     

    And I also agree with Ali Hamdar to use a flag to judge when the code need to be execute once.

     

    We also use invoke method and HtmlElement to input the information into the TextBox in html in WebBrowser.

    Such as, 

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
     this.button1.Text = "completed";
     if (this.webBrowser1.Document.Url.AbsoluteUri == "http://www.bing.com/")
     {
      HtmlDocument htmldoc = this.webBrowser1.Document;
      HtmlElement htmlelement = htmldoc.GetElementById("sb_form_q");
      htmlelement.SetAttribute("Value", "abcdef");
      htmldoc.Forms["sb_form"].InvokeMember("submit");
      MessageBox.Show("submit");
     }
     MessageBox.Show("DocumentCompleted");
    }
    
    

    If there’s anything unclear, please feel free to let me know.

     

    Best wishes,

    Mike

    -------------------------------------------------------------------

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to the other community members reading the thread.

    *****************************************************

    [All-In-One Code Framework]

    Sample world! You will get more from this world!

    Welcome to the new world!

    Sunday, November 14, 2010 12:15 PM
    Moderator
  • I put the sendkeys code in private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

    I realize by placing it here, everytime a new web page loads, it is firing the sendkeys method. However, I have been unable to get the sendkeys method to work anywhere else.

     

    I am working on checking the web url, so the sendkeys will only be sent to the initial page that requires the user id and password.

    thanks

    Monday, November 22, 2010 3:40 PM
  • I amended the code to include the flag:

     

    int flag = 1;

     

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

    {

     

     

     

    while (flag == 1)

    {

     

    SendKeys.Send("userid");

    SendKeys.Send("{TAB}");

     

    SendKeys.Send("password");

     

    SendKeys.Send("{TAB}");

     

    SendKeys.Send("{ENTER}");

    flag = flag + 1;

    }

     

     however, now SendKeys does not work. I have also tried using :

    If flag == 1; but that does not work either.

    I cannot seem to put the SendKeys.Send method anywhere but in

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

    Anywhere else I put it, it will not send keys. I want the program to send the keys to the text boxes built into the web page. I think I may need to send a delay, to wait for the web page to fully load, but am unsure how to do this.

     

    Tuesday, November 23, 2010 6:32 PM
  •  

    Hi mikey814 ,

     

    Have you done it as my post said? I showed you to use SetAttribute method to set the textbox value not use win32 api in this situation.

     

    If there's anything unclear, please feel free to let me know.

     

    Have a nice day!

    Mike

    *****************************************************

    [All-In-One Code Framework]

    Sample world! You will get more from this world!

    Welcome to the new world!

    Friday, November 26, 2010 5:33 AM
    Moderator
  • Hi Mike

    I tried the SetAttribute method. I'm not 100% clear what you are talking about.

    Does the SetAttribute method set the value of the UserId text box on the web page? This is an excerpt from my code:

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

    {

     

    if (this.webBrowser1.Document.Url.AbsoluteUri == https://test.html)

    {

     

    HtmlDocument htmldoc = this.webBrowser1.Document;

     

    HtmlElement htmlelement = htmldoc.GetElementById("CtlLogin1_txtUID");

    htmlelement.SetAttribute(

    "Value", "abcdef");

    }

    This does not seem to work. I believe I have the input id (CtlLogin1_txtUID)correct; I picked this out of the web page source html.This is a secure website, so that may be why the SetAttribute method does not work.

     

    Thanks

    Monday, November 29, 2010 8:03 PM
  •  

    Hi mikey814 ,

    Welcome to MSDN Forums!

     

    You also can comment out the following code line to check if the textBox had set a value:

    htmldoc.Forms["sb_form"].InvokeMember("submit");

     

    And the following html element is what I had used in my solution, so you can make sure you had used the right value for the Element Id in your code.

    <input name="q" title="Enter your search term" class="sw_qbox" id="sb_form_q" type="text"/>

     

    This method can work with bing website, so it is the right solution for your request, but you said it cannot work in your side, so if you also cannot set the value to that textbox, then you can send out that web page address, and let us test it in our side.

     

    If there's anything unclear or anything is not right in my post, please feel free to let me know.

     

    Have a nice day!

    Mike

    *****************************************************

    [All-In-One Code Framework]

    Sample world! You will get more from this world!

    Welcome to the new world!

    Tuesday, November 30, 2010 7:48 AM
    Moderator