Access Textbox and Button inside the form of a frame of a webpage

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • VBAToexcel
    New Member
    • Oct 2013
    • 4

    Access Textbox and Button inside the form of a frame of a webpage

    Dear All,

    I am writing a macro to automate the filling up some data from excel to company website. I have changed the website name in this post for data protection purpose. The excel will login for different clients by using the combination of username and password for respective clients and then some data are required to be inserted in a text box on a web page, I think the text box is on a form and form is within an iframe, within the web page. Once the data is inserted into text box, one button (Submit), which is also on the same form, is to be clicked.

    On the click of a button, the updated data appears on another section, I could not make out if it is an form or frame, which is under the abovementioned form. Once we are happy with the way data appears on the web page, we have to click another button (Update), which is on the same section, to finally updating the data on website.

    I wrote the following code to login to the website and then to navigate to the web page where I have to fill up the performance numbers in a text box.

    The first problem is how to access the text box inside the form from VBA so that the macro can write a number in that text box and how to access the button to submit the data. The HTML code, which can be seen on click of F12, is attached below.

    The second problem is how to access the Update button inside the other section, so that the data will be finally uploaded.

    Code:
    Sub LoginToCorpAccount()    
        Dim ie As Object
        Set ie = CreateObject("InternetExplorer.Application")
      
        With ie
            .Visible = True
            .navigate "https://kvinvest.com/clients/perf/"        
     
            ' "Submit" button is found but it has no name – that is why
            ‘I have gone through all the elements with Tagname “input”                    
     
            Set objcollection = ie.document.getElementsByTagName("input")
     
            i = 0
            While i < objcollection.Length
                Debug.Print objcollection(i).Name
                If objcollection(i).Name = "login" Then        
                    objcollection(i).Value = "silver2"
           
                ElseIf objcollection(i).Type = "password" Then
                    objcollection(i).Value = "golden2"
           
                Else
                    If objcollection(i).Type = "submit" Then        
                        Set objElement = objcollection(i)
                        objElement.Click
                        GoTo NextStep
           
                    End If
                End If
                i = i + 1
            Wend
    NextStep:          
    ‘navigate to the page to upade the data        
    .navigate "https://kvinvest.com/clients/perf/?action=CurrentMonth"
     
    ‘nevigate to the page by further clicking the required link appearing on the above page
                    ie.document.all.Item("server_clientr").Click         
       
        End With
        ie.Quit
        Set ie = Nothing
    End Sub
    The F12 view (HTML) of the web pages shows the following:
    Code:
    <iframe name="PerformanceFrame" id=" PerformanceFrame " src="?action=iframe" frameBorder="0" style="width: 0px; height: 0px;">
    <form class="ssc-form" action="https://kvinvest.com/clients/perf/?action=template&tid=signal_server_consumer&noheader=true" method="post" jQuery1381351885765="3">
    -<input name="call" type="hidden" value="ssc"/>
    -<input name="type" type="hidden" value="subscribe"/>
    -<input name="csrf" type="hidden" value="8888888889999999"/>
    --<div style="margin-bottom: 3px;">
    --<span class="ssc-data-help" rel="ssc-help-provider" jQuery1381351885765="8">
    --<input name="server" id="ssc-subscribe-server" style="width: 150px;" type="text"/>
    --<input name="" id="ssc-subscribe-apply" style="width: 70px;" type="submit" jQuery1381351885765="4" value="Open"/>
    
    
    
    <div id="ssc-consumers-holder"/> -
    This the line which gets highlighted when I click on the section where the data appears after clicking the submit button, and from here Update button is to be clicked.

    Thank you very much for all your help.
    Last edited by zmbd; Oct 10 '13, 03:40 PM. Reason: [Z{Please use the [CODE/] button to format posted code/html/sql/formated text - Please read the FAQ}]
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    PLEASE!
    Make sure that you use the [CODE/] format around ALL HTML, VBA, or other formatted text.

    My company antivirus tripped a warning about this page as a possible IFRAME infection. Once I placed the [Code/] formatting around your posted HTML this warning went away!

    It is very importaint that you comply with this site rule to prevent false positives with other member's and search engine's antivirus software.

    Comment

    • VBAToexcel
      New Member
      • Oct 2013
      • 4

      #3
      Hi ZMBD,

      Next time I will insert the [CODE/] format around all of the codes. I did not realized that I have to insert [CODE/]around all the codes.

      I did not insert [CODE/] in first place as I didn't want that particular piece of codes to appear in that special window.

      But anyway, I will take care from next time.

      Thanks.

      Comment

      • zmbd
        Recognized Expert Moderator Expert
        • Mar 2012
        • 5501

        #4
        You're going to end up working with the DOM (document object model).
        Unfortunately the one working example I have is on the Work PC and I won't have access to it until next Monday.
        This document may help you with some of the basics document object (Internet Explorer)
        I've not tried this with Firefox or other browsers.
        There was a thread on this a few months back that I can find it I'll link it in here...

        Basically, you will add a reference to the IE library and the Microsoft HTML Object Library (if you want to be able to do early bind and use the intelisense and built-in constaints). Then it's like any other office automation script. You'll need to find the element names on the HTML page that hold the data or execute the actions.

        Sorry, I don't have the references here at home.
        Last edited by zmbd; Oct 11 '13, 12:39 AM.

        Comment

        • VBAToexcel
          New Member
          • Oct 2013
          • 4

          #5
          Hi ZmDB,

          Thank you very much for your reply. I appreciate your time and effort.

          I will go through the MSDN link and will try to resolve the issue. I will update the post accordingly.

          But would you please post the working example here once you get hold of it on Monday.

          With best regards.

          Comment

          • zmbd
            Recognized Expert Moderator Expert
            • Mar 2012
            • 5501

            #6
            Document Object Model - Internet Explorer

            VBAToexcel
            -
            I normally wouldn't post anything like the following; however, because you've beaten your head against the wall a few times and so have I in trying to figure out how to directly access webpages from within VBA in general, I came up with the following "Proof of Concept" code some time back and will share my "alpha-code."

            Normally I’d just point you to the sources I found and send you off on your way to figure this out; however, it wasn’t until I found a few non-working examples and figured out what was going on that I came up with the following and finally found some information that actually helped.

            It's not pretty coding... as I said, it is POC work cobbled together from several sources – it worked today, it may not work tomorrow

            What should happen when the code is ran from a standard vba module: late-binds to the internet explorer application and creates the object; navigates to Google (during POC I needed a stable document); waits for the window to open; finds the data entry box and inserts my search string; clicks in the action control; and finally maximizes the window.
            - This code is only valid for IE... I don't know how one would use any of the other browsers; thus, I'll be no help with that!
            Code:
            Option Compare Database
            Option Explicit
            '
            'For latebind IE Object
            Public Enum READYSTATE
                    READYSTATE_UNINITIALIZED = 0
                    READYSTATE_LOADING = 1
                    READYSTATE_LOADED = 2
                    READYSTATE_INTERACTIVE = 3
                    READYSTATE_COMPLETE = 4
            End Enum
            '
            'Need to get at the stuppid API to maximize the window after opening.
            'may break under 64bit installs.
            'borrowed this from somewhere...
            Declare Function apiShowWindow Lib "user32" Alias "ShowWindow" _
                        (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
            
            Global Const SW_MAXIMIZE = 3
            Global Const SW_SHOWNORMAL = 1
            Global Const SW_SHOWMINIMIZED = 2
            
            
            Sub ExampleDocumentObjectModel()
                '
                'based on: http://vba-corner.livejournal.com/4623.html.
                '
                'If you want to use the early binding to have access to all of the
                'nice constants and intilisense then set a reference to:
                'VBA/Editor/Tools/References.../[x]Microsoft Internet Controls
                '
                On Error GoTo errorjumppoint
                Dim z_ie As Object
                '
                'Create a new internet explorer object. The Document above had all sorts
                'of code to check for current IE objects and how to open within.
                'for my needs and as POC just open a new IE
                Set z_ie = CreateObject("InternetExplorer.Application")
                '
                'Let's use a webpage for POC that I know will be there
                z_ie.Navigate "www.google.com"
                '
                'Loop until it has loaded.
                Do Until z_ie.READYSTATE = READYSTATE_COMPLETE
                    'this is an infinite loop... you may want to add a counter and escape.
                    'however, with a know webpage, this shouldn't usually fail.
                Loop
                '
                'Well, lets play with the elements available on the webpage.
                'by inspection of the source, it appears that the following will enter a value into the
                'google search box, escape from the suggested entries, and then click on the execute
                'search
                With z_ie
                    .Visible = True
                    .Document.getElementById("q").Value = "VBA Example"
                    .Document.getElementById("q").Click
                    .Document.getElementById("btnK").Click
                End With
                '
                'Now as I couldn't find a mazimize window property, I'll just use the API call
                'and pass to it the handle for the current IE window.
                apiShowWindow z_ie.hwnd, SW_MAXIMIZE
                '
            errorjumpreturn:
                'release
                If Not z_ie Is Nothing Then Set z_ie = Nothing
                '
            Exit Sub
            errorjumppoint:
                'trap
                MsgBox "Somethin went wrong:" & Err.Number & vbCrLf & Err.Description
                Resume errorjumpreturn
            End Sub
            I want to point out the three ".getrelementby id" lines... you are going to have to find these yourself. by inspecting the HTML for your webpage just as I did for the Google page. Hopefully the livejournal is still up and running as this was the link that finally lead me to the developer's documentation. BTW: some of which is in German, I don't read German; however, I can muddle my way thru it and Google has a translator that works fairly well for technical material.

            Respectfully, I want to make it absolutely clear that:
            Bytes is neither a code writing nor homework service; however, this is such an esoteric question, IMHO, it deserves a little leeway in posting the code.
            Last edited by zmbd; Oct 14 '13, 03:28 PM.

            Comment

            • VBAToexcel
              New Member
              • Oct 2013
              • 4

              #7
              Hi ZMBD,

              Thanks for all your help. I will go through the codes tonight. I will post the result here once again.

              Thanking you once again.

              With best regards.

              Comment

              Working...