dcsimg
Results 1 to 13 of 13

Thread: Select Link in WebBrowser Help

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    43

    Select Link in WebBrowser Help

    Alrighty I happen to be new to visual basic so please don't get too angry with my lack of vast understanding of code. ^_^

    I want to be able to get a link from a web page, let's say google.
    In google I would click a command button or other and it would select a link as cited by the text. By this I mean If I were to go to google and press this button it would display this text in a Textbox
    (http://images.google.com/imghp?hl=en&tab=wi)
    But the button is looking for the text (images) and not a url code.
    How would I go about this? I assume it is a webbrowser1.document.(etc etc) thing but I really don't know how to determine this.

    Thank you in advance to anyone who will help me in this predicament.

  2. #2
    Addicted Member ZenDisaster's Avatar
    Join Date
    Dec 2006
    Location
    Bay Area, CA
    Posts
    140

    Re: Select Link in WebBrowser Help

    Not sure what you named your text box so I just named it txtURL.

    Code:
            Dim MyLink As HTMLLinkElement
    
            For Each MyLink In webbrowser1.document.links
    
                If MyLink.innerText = "Images" Then
    
                    txtURL.text = MyLink.href
                    exit for
    
                End If
    
            Next MyLink
    Last edited by ZenDisaster; Sep 26th, 2009 at 12:40 AM. Reason: Posted code for wrong version.

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    43

    Re: Select Link in WebBrowser Help

    It says user defined-type not assigned.
    what should I do?
    P.S. the textbox is just text4

  4. #4
    Addicted Member ZenDisaster's Avatar
    Join Date
    Dec 2006
    Location
    Bay Area, CA
    Posts
    140

    Re: Select Link in WebBrowser Help

    Hm ya, HTMLLinkElement would require a reference to either the Microsoft HTML Object Library or the IETag library. Try taking away 'As HTMLLinkElement' and see what happens. I don't have VB6 installed cause Im on Win7 and they don't get along very well.

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    43

    Re: Select Link in WebBrowser Help

    Hey I was on windows 7 too, now uninstalling cause although it was good I am way too lazy to try and reinstall my programs... that and the partition wouldn't resize.
    (back to topic) but now it won't recognize anyhing as going wrong but nothing happens, I guess it is because I haven't added the html object library in the first place, what was the code I would need to put in the
    Dim MyLink as HTMLLinkElement
    (The name of the library is scriptlet1)

  6. #6
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Select Link in WebBrowser Help

    The library you are looking for is found under references and called Microsoft HTML Object Library 1.0



    Good Luck
    Option Explicit should not be an Option!

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    43

    Re: Select Link in WebBrowser Help

    Ya, I already got that but it doesn't work with the given code... It won't post into my textbox at all...
    Last edited by Hunterwould; Sep 26th, 2009 at 08:29 AM.

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    43

    Re: Select Link in WebBrowser Help

    My friend said that there was an easier way to use
    Dim AllLinks as String
    AllLinks = Webbrowser1.document.getelementsbytag ("A")
    Text3.Text = AllLinks
    But this only puts the first ("a") in the array can anyone make a loop that would store it in an array?
    Last edited by Hunterwould; Sep 26th, 2009 at 10:09 AM.

  9. #9
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Select Link in WebBrowser Help

    That should be...
    Code:
    Dim AllLinks As Object 'or varient or iHTMLElement (or something like that)
    Dim CurrentLink As Object 'or...
    
    AllLinks = WB.document.getelementsbytag("a")
    
    For Each CurrentLink In AllLinks
      Text1.Text = Text1.Text & CurrentLink.href & vbNewLine
    Next


    Good Luck
    Option Explicit should not be an Option!

  10. #10

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    43

    Re: Select Link in WebBrowser Help

    Has a problem with the second AllLinks...

  11. #11
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Select Link in WebBrowser Help

    Set AllLinks = wb.docuement.getelementsbytagname("a")



    Good Luck
    Option Explicit should not be an Option!

  12. #12

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    43

    Re: Select Link in WebBrowser Help

    Nope, after testing on many websites that code doesn't work for everything, it fails to search an I-Frame.

  13. #13
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Select Link in WebBrowser Help

    You have to navigate to the page within the frame to be able to access the elements contained within, which means ...byname("iframe") or ...byname("frame") and navigate to each page found within by all of the same methods shown above.



    Good Luck
    Option Explicit should not be an Option!

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width