Forum Discussion

mateenhussain's avatar
mateenhussain
Occasional Contributor
15 years ago

Clear text before doing Send Keys

Hi



I am trying to edit a textbox. At first I used testObj.wText property to set text on the textbox. But I had to move to testObj.SendKeys() method as wText didnt work on all platforms, due to user privilege.




The problem I am facing with SendKeys is, it doesn't clear any existing text before sending text to the textbox. It always starts typing from position 0 (i.e beginning).




1) Is the above behaviour expected?




2) How can I clear existing text of the textbox, before sending in keystrokes? I tried testObj.Keys("[Clear]"); method, but it didn't help me much.




Thanks,

Mateen

9 Replies

  • Mateen



    If you want to use keys method only....

    then uses 

    Control.dblCllick

    Control.keys("^A") 

    Control.keys("[BS]")

    before feeding in the data.

    This should clear the field you want to edit.




    Thankyou

  • hagai1973's avatar
    hagai1973
    Occasional Visitor

    'Function received the text box object

     

    Function ClearTextBox(outTxtObj)
      If outTxtObj.text <> "" Then
           ClearTextBox = False

           'Locate mouse in the left corner of edit box

           outTxtObj.Click 5,10
           For i = 0 to Len(outTxtObj.text)
               outTxtObj.keys("[Del]")
           Next
      End If


      If outTxtObj.text = "" Then
           ClearTextBox = True
           Log.Message("Edit box was cleared")
       End If
    End Function

    • kirk_bottomley's avatar
      kirk_bottomley
      Contributor

      Nice one, Hagai.

       

      I've got one in JScript I use before sending keys to a text object. Pass in whatever property you want to search for the text field with (I usually use Name, frequently with wildcards), the parameter, and the process. It also has to be a visible object.

      function ClearTextObject(ByProperty, Parameter, ProcessName)
      {
      var PropArray, ValuesArray, Process, Target;
      var Depth = 5;
        
      PropArray = new Array(ByProperty, "Visible");
      ValuesArray = new Array(Parameter, true);
      
      Process = Sys.Process(ProcessName);
      Target = Process.FindChild(PropArray, ValuesArray, Depth);
      
      try
      {
        if (Target.Exists)
        {
          var Text = Target.Text;
          var Length = Text.length;
          
          Target.Click();
          Target.Keys("[End]");
          
          Indicator.PushText("Backspacing the text away.");
          for (i = 0; i < Length; i++)
          {
            Target.Keys("[BS]");
          }
          Indicator.PopText();
        }
        else
          Log.Error("The object was not found.");
      }
          
      catch (err)
       {
        Log.Error(err.description)
       }
      }
      • jsc's avatar
        jsc
        Regular Contributor

        you can try

        Obj.Keys("^aMyText")

        so all text is selected (^a) and replaced withy "MyText"

         

        Or Obj.SetText("MyText")

  • NisHera's avatar
    NisHera
    Valued Contributor

    usualy I do is 

    Object.SetText("");

    Object.Keys("What ever the text to be keyed")

    • Kamal_chennai's avatar
      Kamal_chennai
      Contributor

      Hi,

       

      It's always better to use Keys method instead of SetText

       

      You can Try with the following

       

      Object.Keys("[End]![Home][Del]"+Project.Variables.XXX); or Object.Keys("[Home]![End][Del]"+Project.Variables.XXX);

       

      Also Goto Project Properties -> Playback, There is one textbox available "Key Pressing Delay, MS" You can use 50ms or 25ms instead of 0

       

      It may helpful :)

       

      Thanks,

      Kamal

      • Ravik's avatar
        Ravik
        Super Contributor

        Hi,

         

        Use  objName.value = "" .

         

        sometime keys and settext methods may append blank value in your object (if object is auto filled, Which is having water mark text) in this case .value method is most usefull.

         

         

  • m_essaid's avatar
    m_essaid
    Valued Contributor

    a simple object.DblClick() works fine to "select all"... then use directly ".Keys()" because the hit replace the selection.