Results 1 to 11 of 11

Thread: Filling web forms

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Location
    Kerala, India
    Posts
    275

    Filling web forms

    Hi,

    I want to know how to how to fillup a web form programmatically using VB.

    For example I want to fill data in the following page (image attached) and click the 'go' buttom programmatically. Then I want to save the URL of the resulting page to a string variable.

    Thanx in advance.
    Attached Images Attached Images  

  2. #2
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Filling web forms

    You can use the Internet Explorer Object (DOM If i am correct).
    example : Filling GoOogle search box and click the search button.
    Code:
    Private Sub Command1_Click()
    Dim oIE As Object
    
    Dim URL As String
    
    
    Set oIE = CreateObject("InternetExplorer.Application")
    URL = "http://www.google.com"
    oIE.Visible = True
    oIE.navigate (URL)
    
    You need to find the name of the textbox & the name fo the "Submit" button of your webpage.
    I recommed you to use "IE Developer Toolbar" to find hease items on the webapge.
    Do While oIE.readyState <> 4 And oIE.Busy
        DoEvents
    Loop
    
    oIE.Document.All.Item("q").Value = "Football"
    oIE.Document.All.Item("btnG").Click
    
    End Sub
    You need to find the name of the Textbox & the name of the Submit button of the webpage shown in your picture. If you have installed "IE Developer Toolbar" it is very easy to find eliments on the webpages.
    Last edited by Fazi; Jan 14th, 2008 at 03:15 AM.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Location
    Kerala, India
    Posts
    275

    Re: Filling web forms

    Thank you FAzi ,

    I have tried as you told. But I get an error "Object does not support this type of ....". What could be wrong? I give below the code.

    Private Sub Command1_Click()
    Dim oIE As Object
    Dim URL As String

    Set oIE = CreateObject("InternetExplorer.Application")
    URL = "http://www.geojit.com/quoteslong.asp?pageOpt=1&index=6"
    oIE.Visible = True
    oIE.Navigate (URL)

    Do While oIE.ReadyState <> 4 And oIE.Busy
    DoEvents
    Loop

    oIE.Document.All.Item("txtQuotes").Value = "UCO Bank" '<===It gets stuck here
    oIE.Document.All.Item("Image1").Click

    End Sub

  4. #4
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Filling web forms

    OK , can you give that URL which shows the textbox and the button.

    i thing the item names you have put is not correct.
    Last edited by Fazi; Jan 14th, 2008 at 06:32 AM.

  5. #5
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Filling web forms

    I think the problem might be that "txtQuotes" appears a number of times and you have to be a bit more specific. ie
    Code:
    oIE.Document.Forms("form2").Item("txtQuotes").Value = "UCO Bank"

  6. #6
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Filling web forms

    Yes Doogle, might be ..

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Location
    Kerala, India
    Posts
    275

    Re: Filling web forms

    Quote Originally Posted by Fazi
    OK , can you give that URL which shows the textbox and the button.

    i thing the item names you have put is not correct.
    The URL is "http://www.geojit.com/quoteslong.asp?pageOpt=1&index=6"

  8. #8
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Filling web forms

    oh, this will going to have some serious privacy issues.
    when i click that link, it asks for a code "Enter the first few characters of the company's name or the NSE symbol or BSE code and click ' Go ' "

    * Please don't post your personal account informations in the forum !!!!

    Did you check that text box name and the button name with "IE Developer Toolbar" ?
    first please make sure the names are correct. you also should try Doogles suggession.
    Last edited by Fazi; Jan 15th, 2008 at 12:40 AM.

  9. #9
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Filling web forms

    Don't panic, Fazi - it's a stock quotation site

    My earlier suggestion works for me. However, I noticed that
    Code:
    Do While oIE.ReadyState <> 4 And oIE.Busy
    DoEvents
    Loop
    was unreliable as a method of determining whether the Document had completely loaded. In some instances it worked ok and in others I received the traditional "Object variable with block variable not set" error message, meaning that the document wasn't ready. I'm not sure if there's a better method when you're creating an instance of Explorer as you are. Perhaps others will know.

    A suggestion might be to use the WebBrowser control where you can use various events - in particular the DocumentComplete event which is triggered when a URL is loaded.
    eg
    Private Sub wb1_DocumentComplete(ByVal pDisp As Object, URL As Variant)

    End Sub

    Since this event is passed the URL being completed you can compare that to the one requested and if there's a match you know that it's ready.

    Even with that method you have to be very careful. For example, navigating to http://www.vbforums.com causes 2 http requests to be made and therefore there will be 2 DocumentComplete events triggered, but the URL returned in the DocumentComplete event has a trailing "/" added. Also the order the requests are completed may be different from the order they were made.

    The code below demonstrates:
    Code:
    Private strURL As String
    Private boLoaded As Boolean
    
    Private Sub Command1_Click()
    Dim intI As Integer
    strURL = "http://www.vbforums.com/"
    boLoaded = False
    wb1.Navigate strURL
    Do Until boLoaded = True
        intI = intI + 1
        If intI = 1000 Then
            DoEvents
            intI = 1
        End If
    Loop
    '
    ' We can be reasonably sure that the document is now loaded
    '
    End Sub
    
    Private Sub wb1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    Static intCount As Integer
    intCount = intCount + 1
    Debug.Print "DocumentComplete " & intCount & " " & URL
    If URL = strURL Then boLoaded = True
    End Sub
    
    Private Sub wb1_DownloadBegin()
    Static intCount As Integer
    intCount = intCount + 1
    Debug.Print "DownLoadBegin " & intCount
    End Sub
    
    Private Sub wb1_DownloadComplete()
    Static intCount As Integer
    intCount = intCount + 1
    Debug.Print "DownLoadComplete " & intCount
    End Sub
    
    Private Sub wb1_NavigateComplete2(ByVal pDisp As Object, URL As Variant)
    Static intCount As Integer
    intCount = intCount + 1
    Debug.Print "NavigateComplete " & intCount & " " & URL
    End Sub
    I've coded up some of the other navigation related events just to indicate the order of things happening.

    If you run this, you'll see that even though the forum's main page was requested first, it doesn't complete until the second DocumentComplete event. So if you don't check which URL has completed and take action on the first DocumentComplete event you run the risk of an error being generated when you try to access the Document. (But, experience shows, it is timing dependent so it wont fail all of te time!!)

    One other little "feature" of the WebBrowser control you have to watch out for is, that if you set it's Visible property = False, the DocumentComplete event never triggers ! I think this control was written on a Friday afternoon after a serious session at the local pub
    Last edited by Doogle; Jan 15th, 2008 at 02:11 AM.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Location
    Kerala, India
    Posts
    275

    Re: Filling web forms

    Quote Originally Posted by Doogle
    I think the problem might be that "txtQuotes" appears a number of times and you have to be a bit more specific. ie
    Code:
    oIE.Document.Forms("form2").Item("txtQuotes").Value = "UCO Bank"
    It works now. Thanx.

    But I want the circled figure in this page (attached image). How do I get it without knowing the URL?

    Or alternately, can I get the URL of this page?
    Attached Images Attached Images  

  11. #11
    Hyperactive Member Ambivalentiowa's Avatar
    Join Date
    Apr 2002
    Location
    Coming soon to a store near you!
    Posts
    375

    Re: Filling web forms

    If it will always be in the same location on the page, you can use something like the following:

    vb Code:
    1. webinfo = oIE.Document.all.tags("td").Item(19).innerText

    Just mess around with the Item value till you grab the right cell.
    -Show me on the doll where the music touched you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width