0

I am having trouble finding a link in an html page. As a way find the link, I created some code which dumps all outerhtml or innerhtml elements based on which you choose to send to the sub. It works fine, but for some reason will not reflect the link I need. Therefore, I cannot see the element I want to click.

The web page has a column to the left. My tag dump sub doesn't seem to go beyond that to get the main page tags.

Tried dozens of ways to capture the elements, but had no success.

On the webpage documentcompleted event, run this code:

    For Each link As HtmlElement In links
        DumpTags(link.OuterHtml.ToString)
        'DumpTags(link.innerHtml.ToString)


        If link.InnerHtml.IndexOf("Correspondence") <> -1 Then
            link.InvokeMember("Click")
        End If
    Next

Private Sub DumpTags(link As String) 'write a text file with all htmlelements

    Dim strwriter As IO.StreamWriter
    strwriter = New IO.StreamWriter("C:\temp\tags.txt", True)
    strwriter.WriteLine(link)
    strwriter.Close()
End Sub

Expected behavior: The code works but it doesn't seem to go deep enough. I want to get all html links including (in this case) a link with "Correspondence" as a tag name.

BradI
  • 11
  • 3
  • 1
    [How to get an HtmlElement value inside Frames/IFrames?](https://stackoverflow.com/a/53218064/7444103) – Jimi Aug 25 '19 at 00:17
  • A little bit of recursion might be in order here. But in any case you're not showing us how/where you're getting the `links` collection. We'll need to see all of the relevant and supporting code before we can help. That said, two other options are 1) [Selenium XPath expressions](https://www.edureka.co/blog/xpath-in-selenium/) and 2) [Html Agility Pack](https://html-agility-pack.net/). – InteXX Aug 25 '19 at 08:21
  • 1
    @InteXX `[WebBrowser].Document.Links` provides a `Links` collection, which already references all the links inside a HtmlDocument. Point is, the *main* Document may contain a number of other sub-Documents, inside Frames, IFrames and also outside. You need to find out which Document contains the Links you're after. The standard objects offer more then enough tools to complete this task, without the need of other packages. – Jimi Aug 25 '19 at 08:50
  • @Jimi ~ Got it, thanks for the update. – InteXX Aug 25 '19 at 13:08
  • @Jimi, Thanks. This brings me closer. I did find that yes, the links I am looking for are buried in an iframe. I am struggling to find out how to reference it directly (I'm rusty with C#). – BradI Aug 26 '19 at 15:35
  • You have to find the Element you're after, then you can reference it/use it/`Invoke` it (when you have found it, it means it exists, at that point). I linked that answer in the first comment for a reason. – Jimi Aug 26 '19 at 15:38
  • Thanks again, I got it working by doing as it instructed. Pulling the frame and then finding the elements there. Unfortunately, there's something behind that link which makes the page perpetually refresh. I tried manually navigating to the page using the webbrowser control and still clicking that link causes a perpetual refresh. – BradI Aug 26 '19 at 23:47

0 Answers0