VBscript Check Box Form Fill Question

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

VBscript Check Box Form Fill Question

Post by Rain » Tue May 11, 2010 3:01 pm

How do I check a Check Box using this example http://www.mjtnet.com/forum/viewtopic.php?t=1461

I want to check the "Keep me logged in" check box at http://www.facebook.com/login.php

I know the name (persistent) and value (1) but I can't get the script to check the check box.

Thanks for any help.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue May 11, 2010 3:13 pm

Send a space to it:

Press Tab
Send>space
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Post by Rain » Tue May 11, 2010 3:17 pm

Thank you for your quick reply. I'm currently using image recognition but I was wondering if there is a way to check it using a VBscript instead. It would make it more reliable across multiple operating systems and browser settings.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue May 11, 2010 3:27 pm

You can use the tag's click method. Something like:

Code: Select all

Sub ClickIt(fieldname)
  Dim FormNr
  Dim ItemNr
  Dim TheForm

  if IE.Document.All.Tags("FORM").Length = 0 then
    MsgBox("No form found in page")
  else
    for FormNr = 0 to IE.Document.Forms.Length - 1
      Set TheForm = IE.Document.Forms(FormNr)
      for ItemNr = 0 to TheForm.Elements.Length - 1
        if TheForm.Elements(ItemNr).Name = fieldname then
          TheForm.Elements(ItemNr).Click
        exit for
        end if
      next
    next
  end if
End Sub
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Post by Rain » Tue May 11, 2010 4:05 pm

Thanks again.

I must be doing something wrong because VBEval>ClickIt(persistent) is not "clicking" the check box. :?

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue May 11, 2010 4:08 pm

I haven't actually tested the code I posted, but you're calling it wrong anyway. It's a Sub, so you should use:

VBRun>ClickIt,Persistent
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Post by Rain » Wed May 12, 2010 2:11 pm

Thank you for all your help, Marcus.

Here is the working script if anyone is interested.

Code: Select all

VBSTART
Dim IE

Sub CreateIE
  Set IE = CreateObject("InternetExplorer.Application")
  IE.Visible=1
End Sub

Sub Navigate(URL)
  IE.Navigate URL
  do while IE.Busy
  loop
End Sub

Sub KillIE
  IE.Quit
  Set IE = nothing
End Sub

Sub WebFormFill(fieldname,fieldvalue,submit)
  Dim FormNr
  Dim ItemNr
  Dim TheForm

  if IE.Document.All.Tags("FORM").Length = 0 then
    MsgBox("No form found in page")
  else
    for FormNr = 0 to IE.Document.Forms.Length - 1
      Set TheForm = IE.Document.Forms(FormNr)
      for ItemNr = 0 to TheForm.Elements.Length - 1
        if TheForm.Elements(ItemNr).Name = fieldname then
          TheForm.Elements(ItemNr).Value = fieldvalue
          If submit=1 then
            TheForm.submit
          end if
          exit for
        end if
      next
    next
  end if
End Sub

'This function extracts text from a specific tag by name and index
'e.g. TABLE,0 (1st Table element) or P,1 (2nd Paragraph element)
'set all to 1 to extract all HTML, 0 for only inside text without HTML
Function ExtractTag(TagName,Num,all)
  dim t
  set t = IE.document.getElementsbyTagname(Tagname)
  if all=1 then
    ExtractTag = t.Item(Num).outerHTML
  else
    ExtractTag = t.Item(Num).innerText
  end if
End Function


Sub ClickIt(fieldname)
  Dim FormNr
  Dim ItemNr
  Dim TheForm

  if IE.Document.All.Tags("FORM").Length = 0 then
    MsgBox("No form found in page")
  else
    for FormNr = 0 to IE.Document.Forms.Length - 1
      Set TheForm = IE.Document.Forms(FormNr)
      for ItemNr = 0 to TheForm.Elements.Length - 1
        if TheForm.Elements(ItemNr).Name = fieldname then
          TheForm.Elements(ItemNr).Click
        exit for
        end if
      next
    next
  end if
End Sub
VBEND




VBRun>CreateIE
VBRun>WaitBusy
VBRun>Navigate,http://www.facebook.com/login.php
VBRun>WaitBusy
wait>1
VBRun>WebFormFill,email,Email Address,0
VBRun>WebFormFill,pass,Password,0
VBRun>ClickIt,persistent

VBRun>WebFormFill,login,Login,1
VBRun>WaitBusy

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts