Search code examples
excelvbaurlinternet-explorerweb-scraping

Navigate readystate gives a ready state while not ready


I have code to open a site and search for a specific gene (examples for testing mentioned: AXl, TREM2).

If I run the code multiple times and/or change the gene I am searching for, it crashes at:

brow.document.forms("searchForm").elements("query").Value = geneN

It seems the navigate function gives a readystate while not ready.

I put a wait of 10 sec but it seems to skip a correct navigate.

Sub openprot()

    Dim brow As New SHDocVw.InternetExplorer
    Dim URLp As String
    Dim geneN As String 'name of gene
    Dim HTMLco As HTMLDocument
    Dim allrefs As MSHTML.IHTMLElementCollection
    Dim elink As MSHTML.IHTMLElement
    Dim lol As String
    
    geneN = Application.InputBox("name your gene", "gene") 'try TREM2 or AXL
    
    URLp = "https://www.proteinatlas.org/"
    
    brow.navigate URLp
    Do Until brow.READYSTATE = 4: DoEvents: Loop 'variation tested
    'Do While brow.READYSTATE <> READYSTATE_COMPLETE: DoEvents: Loop
    'Do While brow.Busy: DoEvents: Loop
    'Do Until brow.READYSTATE = READYSTATE_COMPLETE: DoEvents: Loop
    
    brow.document.forms("searchForm").elements("query").Value = geneN
    brow.document.forms("searchForm").elements("searchButton").Click
    
    Set HTMLco = brow.document
    Application.Wait Now + TimeValue("00:00:02")
    Set allrefs = HTMLco.getElementsByTagName("a")
    
    For Each elink In allrefs
        lol = elink.href
        If InStr(1, lol, geneN, 1) > 0 And InStr(1, lol, "tissue", 1) > 0 Then
            elink.Click
            Application.Wait Now + TimeValue("00:00:02")
            brow.Visible = True
            Exit For
        End If
    Next elink
    
End Sub

Solution

  • I found that IE is not always loading page (eg. 404 type errors) This will give a ready state but ofcourse all the items of the page are not there.

    To subvert this i load the page until the item is their as suggested by Tim Williams`. I included it into a loop until the page is loaded Please be carefull with this loop as a loop counter might be usefull

    See code below

    Dim brow As New SHDocVw.InternetExplorer
    Dim URLp As String
    Dim GeneB As Object 'button used
    Dim geneN As String 'Gene name
    Dim HTMLco As HTMLDocument
    Dim allrefs As MSHTML.IHTMLElementCollection
    Dim elink As MSHTML.IHTMLElement
    Dim lol As String
    Dim x As Integer
    
        Set GeneB = ActiveSheet.Buttons(Application.Caller)
        geneN = GeneB.Characters.Text
            x = 0
        URLp = "https://www.proteinatlas.org/"
        Do Until x = 1
            brow.navigate URLp
            Do While brow.READYSTATE <> READYSTATE_COMPLETE: Loop
            If IsObject(brow.document.forms("searchForm")) Then
                x = 1
            End If
        Loop
        brow.document.forms("searchForm").elements("query").Value = geneN
        brow.document.forms("searchForm").elements("searchButton").Click
        
        Set HTMLco = brow.document
        Application.Wait Now + TimeValue("00:00:02")
        Set allrefs = HTMLco.getElementsByTagName("a")
        
        For Each elink In allrefs
        lol = elink.href
            If InStr(1, lol, geneN, 1) > 0 And InStr(1, lol, "tissue", 1) > 0 Then
                elink.Click
            Application.Wait Now + TimeValue("00:00:02")
            brow.Visible = True
            Exit For
            End If
        Next elink