Announcement

Collapse
No announcement yet.

Click URL links with VBA

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Click URL links with VBA

    In the past few days I was working on a VBA project which needs automation to the website. However, this one is harder than I thought and I donít know how and where to start it.

    This is the website I am trying to login automatically (https://defben.retirementpartner.com...te/default.asp). This is built by a vendor and they used a lot of frames, JavaScript and VBScript. As you can see on the website and what I want is to click that Login button. I've tried to loop through the tags (seems like this link doesnít have a tag), grab the frames (I found total 6 frames but couldnít find where the linkage have this Login button), and use F12 to view the source code just to see if there is a way I can make my mouse to click that button but in vain.

    Can anyone help me to write an Excel VBA code so when the user click a button, it will click the Login on the 1st page and then the very first click here button on the 2nd page?

    Thanks for the time,

    Ian

    P.S. If you see a picture when you open the website, just click any of them and it will direct you to the page with that Login button.

    This is also posted here (http://www.mrexcel.com/forum/excel-q...lications.html).

  • #2
    Re: Click URL links with VBA

    Ian

    I tried to find a way to do this yesterday(?) but came to a dead end.

    There's seems to be frames within frames within frames.

    I'll give it another go with a more brute force method.
    Boo!

    Comment


    • #3
      Re: Click URL links with VBA

      This bypasses the frames and opens the document contained in the menu frame on it's own.
      Code:
      Sub test()
      Dim IE As InternetExplorer
      Dim doc As HTMLDocument
      Dim frm As HTMLFrameElement
      Dim frms As HTMLElementCollection
      Dim strSQL As String
      
          strSQL = "https://defben.retirementpartner.com/membersite/globalresources/menu/Menu.asp"
          
          Set IE = CreateObject("InternetExplorer.Application")
          With IE
              .Visible = True
              .Navigate strSQL
      
              Do Until .ReadyState = 4: DoEvents: Loop
              Set doc = IE.Document
              
              doc.getElementsByTagName("A")(0).Click
              
              
      
          End With
      End Sub
      Boo!

      Comment


      • #4
        Re: Click URL links with VBA

        Thanks, Norie. You are everywhere helping people. Very nice of you. Yes, you did help me yesterday and I am still searching around to see if there is an answer.

        I know they built many frames and I couldn't really follow their codes. Please let me know if you need anything so we can tackle this down.

        Thanks again and have great day.

        Ian

        Comment


        • #5
          Re: Click URL links with VBA

          Thank you, Norie. I just tried your code and it seems we are getting something.

          A quick note here. I tried the way by getting the URL with Login words only page (the Menu.asp) before, and this actually won't let me login properly. Instead it will ask me to update the password and nothing will happen. I am thinking to retrieve all the tag names and inner texts to see if there is a way to click that Login word. Do you think you can find a way to use getElementsByTagName and find all the tags? I tried getElementsByTagName("*") and loop but I got an error. Maybe I didn't declare the syntax correctly. Please let me know if you can help me.

          Thanks,

          Ian

          Comment


          • #6
            Re: Click URL links with VBA

            Ian

            GetElementsbByTagName/ID/Classname was the sort of thing I was trying yesterday.

            I kind of guessed going straight to the page wouldn't be ideal, it's just hit or miss if it is.

            I'll have another look at things tomorrow.
            Boo!

            Comment


            • #7
              Re: Click URL links with VBA

              From the very first page, which company are you wanting to log in for?
              WebsterDeMonstration&Associates
              SunTrust
              USEppersonUnderwritingCompany
              GwinnettCounty
              Great-WestLife&Annuity
              CanadaLifeAssuranceCo
              Gwrs

              Comment


              • #8
                Re: Click URL links with VBA

                Hi sjeter,

                This page will be only displayed when you first visit the site. You can click any of them just to pass it. Then the next page is what I am working on.

                Thanks,

                Ian

                Comment


                • #9
                  Re: Click URL links with VBA

                  Hi Norie,

                  I modified the code as below. But how do I find all the tags? Is there any wildcard I can use to replace "A"? I tried "*" but it doesn't work. Not sure what went wrong.

                  And from trying the code below, I didn't see the link that I can click on the "Login" word.

                  Code:
                  Option Explicit
                  
                  
                  Sub test()
                      Dim IE As InternetExplorer
                      Dim doc As HTMLDocument
                      Dim frm As HTMLFrameElement
                      Dim frms As HTMLElementCollection
                      Dim strSQL As String
                      Dim i As Integer, j As Integer
                      
                      strSQL = "https://defben.retirementpartner.com/membersite/default.asp"
                       
                      Set IE = CreateObject("InternetExplorer.Application")
                      With IE
                          .Visible = True
                          .Navigate strSQL
                           
                          On Error Resume Next
                          Do Until .ReadyState = 4: DoEvents: Loop
                          Set doc = IE.Document.frames
                          For i = 0 To doc.Length - 1
                              For j = 0 To 100 ' Not sure how to find the last tag name
                                  Debug.Print doc(i).getElementsByTagName("A")(j) ' Not sure if there is a wildcard to replace "A"
                              Next
                          Next
                      End With
                  End Sub

                  Comment


                  • #10
                    Re: Click URL links with VBA

                    Ian

                    I've tried similar code and the main problem was that the frame with the link is within one of the frames on the main document.

                    I've managed to get a reference to the right frame but I'm not referencing it 'properly'

                    By 'properly' I mean in a way that allows you to access the link.
                    Boo!

                    Comment


                    • #11
                      Re: Click URL links with VBA

                      I can find the 1st tier frames (total 6 of them). But do you know any code that I can find sub-frames?

                      Comment


                      • #12
                        Re: Click URL links with VBA

                        I have code to get the frame with the link but it's not right, see my previous post.

                        I could post it but I don't think it would be of much use.
                        Boo!

                        Comment


                        • #13
                          Re: Click URL links with VBA

                          Sure, Norie, please post it so I can at least learn something cool.

                          Comment


                          • #14
                            Re: Click URL links with VBA

                            Ian

                            It's far from cool - it doesn't even do what I want it to do.

                            You could probably do it yourself.

                            Just take a look at the frames property of the document and each of the frames.
                            Boo!

                            Comment


                            • #15
                              Re: Click URL links with VBA

                              I've been a bit stupid, it's easier than I thought and I've actually done this sort of thing before quite a few times.

                              Just needed my memory jogged, which it was when I saw some code for working with frames.

                              This is rough, but I'm sure you can tidy it up.
                              Code:
                              Dim IE As InternetExplorer
                              Dim doc As HTMLDocument
                              Dim doc2 As HTMLDocument
                              
                              Dim frm1 As HTMLFrameElement
                              Dim frm2 As HTMLFrameElement
                              Dim lnk As HTMLLinkElement
                              Dim strSQL As String
                              
                                  'strSQL = "https://defben.retirementpartner.com/membersite/globalresources/menu/Menu.asp"
                                  strSQL = "https://defben.retirementpartner.com/membersite/default.asp"
                              
                                  Set IE = CreateObject("InternetExplorer.Application")
                                  With IE
                                      .Visible = True
                                      .Navigate strSQL
                              
                                      Do Until .readyState = 4: DoEvents: Loop
                                      Set doc = IE.document
                              
                                      Set doc2 = doc.frames("fracenter").document
                              
                                      Set doc2 = doc2.frames("menu1").document
                              
                                      Set lnk = doc2.getElementsByTagName("A")(0)
                              
                                      lnk.Click
                                      
                                  End With
                              Oops, just noticed I've called the string strSQL.
                              Boo!

                              Comment

                              Working...
                              X