How to Click a link on webpage
Results 1 to 2 of 2

Thread: How to Click a link on webpage
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Aug 2019
    Posts
    1
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default How to Click a link on webpage

    I am unexperienced when it comes to navigating websites using VBA (or any code). I have been able create code to open the desired websites but now I am trying to automate "clicking a link" to save additional time.

    I have an excel file full of various patent numbers. I have been able to create code that will open each of the patents within google.patents.
    For example https://patents.google.com/patent/US10000000

    Now I would like to automate clicking on the "Download PDF" link.

    I am not sure where to start. Any help would appreciated.

    Thx,
    --Rezach20

  2. #2
    Board Regular lrobbo314's Avatar
    Join Date
    Jul 2008
    Location
    California
    Posts
    2,381
    Post Thanks / Like
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)

    Default Re: How to Click a link on webpage

    Try this. Instead of clicking the link, this gets the href from the link and downloads it to the desktop. Make sure to change the commented line to where you want the files saved. Also, the code can be amended to loop through several patent numbers. This one just does the one that you gave in the OP.

    Code:
    Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" _
        Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, _
        ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
    
    Sub DownloadPDF()
    Dim IE As Object: Set IE = CreateObject("InternetExplorer.Application")
    Dim URL As String: URL = "https://patents.google.com/patent/US10000000"
    Dim SP() As String: SP = Split(URL, "/")
    Dim pNum As String: pNum = SP(UBound(SP))
    Dim Path As String: Path = "C:\Users\USERNAME\Desktop\" 'Change to where you want the file to be saved
    Dim Link As Object
    Dim HTML As Object
    
    IE.navigate (URL)
    
    Do While IE.busy = True Or IE.readystate <> 4
        DoEvents
    Loop
    
    Set HTML = IE.document
    Set Link = HTML.querySelector("#wrapper > div:nth-child(2) > div.flex-2.style-scope.patent-result > section > header > div > a")
    downloadFile Link.href, Path, pNum
    Set IE = Nothing
    End Sub
    
    Sub downloadFile(URL As String, Path As String, FileName)
    URLDownloadToFile 0, URL, Path & FileName & ".pdf", 0, 0
    End Sub
    To add code to a workbook. Hit Alt+F11. Hit Alt+I+M to insert new module. Then paste code.
    Array formulas must be entered by hitting Ctrl+Shift+Enter.

    We can't solve problems by using the same kind of thinking we used when we created them.

    Imagination is more important than knowledge.

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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