0

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
Community
  • 1
  • 1
Waly
  • 46
  • 5
  • 1
    Just because the page is done "navigating" doesn't mean it's fully-loaded/complete. Modern pages often have a lot of post-load dynamic content which can take some time to finish building. You might need to put a loop to check for the presence of the item you need. Eg: https://stackoverflow.com/questions/47455109/how-to-wait-for-the-page-to-load-after-click – Tim Williams Apr 15 '21 at 16:39
  • Blessed are they, the god of stack overflow! – Waly Apr 15 '21 at 17:03
  • @Waly, is your issue resolved now? If yes, I suggest you post your solution as an answer to this question and accept it after 48 hrs. If the issue still persists, please inform the current status of the issue. Thanks for your understanding. – Deepak-MSFT Apr 16 '21 at 05:51

1 Answers1

0

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
Waly
  • 46
  • 5