Results 1 to 5 of 5

Thread: Get href of a button through web browser control

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2013
    Posts
    108

    Get href of a button through web browser control

    I'm trying to get the href of a button using the control Web Browser. The html is:

    HTML Code:
    <a download="" href="https://x638jxfnvmcxbaxcskqsdadskdqnfwexc.shikisha.shiksha/f2xcqrz1wxf13dssdalkgrdjkgdklfjdsaklkcxmsacxzcdfsvsdcqwdjkkvfmdskicmsdascmamzcassmdamcxscxzcsacasvfdssacxzcsdxvsaqswcxzvbededvvb.php?nqusjmslpeprtmdsjkhghewojicwe0guhifkjbh2qqqhstexrsfiwhvfmmoorewioujgbhuiovshdkjwvcjhfdbwehjkvkjhsdnvmwouipfdwxjwsfiwhvfhlmj=yyhnlsoahifiztdsjkhghewojicwe0guhifkjbh2qqqhscscvsfiwhvffbbsrewioujgbhuiovshdkjwvcjhfdbwehjkvkjhsdnvmwouipfdryansfiwhvfmwdn222811prciwnbztwy5&amp;pxhotisnweqwlkdsjkhghewojicwe0guhifkjbh2qqqhsotjpsfiwhvfnshnrewioujgbhuiovshdkjwvcjhfdbwehjkvkjhsdnvmwouipfdnpsesfiwhvfoczq=yloyeevwdfcbpkdsjkhghewojicwe0guhifkjbh2qqqhszjdssfiwhvfeyczrewioujgbhuiovshdkjwvcjhfdbwehjkvkjhsdnvmwouipfdozixsfiwhvfhcimfslzpcikfrvnqldsjkhghewojicwe0guhifkjbh2qqqhsptjesfiwhvfmzsrrewioujgbhuiovshdkjwvcjhfdbwehjkvkjhsdnvmwouipfdyskesfiwhvfcchh&amp;fkdsadxcwaqcscasxcsacxsaxazxcvsdvdslssafdsvx=174147325&amp;dkjfsjksdadsdvcxxcvswscxrewsdf=sdx2&amp;sdwreggllslsdxvc&amp;sadklxcklkvb=qwrdcbkklsdxk&amp;jiijlks=lz9fSJUhlRoBL8M8qh4Ido&amp;uijdsdkdfkgfdsd=85652262237b1335ca34.14415852&amp;lkfdsfxc=lz9fSJUhlRoBL8M8qh4Ido&amp;nqusjmslpeprtmdsjkhghewojicwe0guhifkjbh2qqqhstexrsfiwhvfmmoorewioujgbhuiovshdkjwvcjhfdbwehjkvkjhsdnvmwouipfdwxjwsfiwhvfhlmj=yyhnlsoahifiztdsjkhghewojicwe0guhifkjbh2qqqhscscvsfiwhvffbbsrewioujgbhuiovshdkjwvcjhfdbwehjkvkjhsdnvmwouipfdryansfiwhvfmwdn222811prciwnbztwy5&amp;pxhotisnweqwlkdsjkhghewojicwe0guhifkjbh2qqqhsotjpsfiwhvfnshnrewioujgbhuiovshdkjwvcjhfdbwehjkvkjhsdnvmwouipfdnpsesfiwhvfoczq=yloyeevwdfcbpkdsjkhghewojicwe0guhifkjbh2qqqhszjdssfiwhvfeyczrewioujgbhuiovshdkjwvcjhfdbwehjkvkjhsdnvmwouipfdozixsfiwhvfhcimfslzpcikfrvnqldsjkhghewojicwe0guhifkjbh2qqqhsptjesfiwhvfmzsrrewioujgbhuiovshdkjwvcjhfdbwehjkvkjhsdnvmwouipfdyskesfiwhvfcchh"><button id="myImageId44" type="button" class="download2 download2dahover"><font color="#F08080">Download MP3 (Bitrate: 320 kbps | 7.28 MB | 00:03:10)</font></button></a>
    The code I'm trying inside the event Document Completed of the Web Browser control is:
    Code:
    Dim HtmlCollec As HtmlElementCollection
             HtmlCollec = WebBrowser1.Document.GetElementsByTagName("button")
             For Each iElement As HtmlElement In HtmlCollec
                 If iElement.GetAttribute("id").Equals("myImageId44") Then
                     RichTextBox1.Text = iElement.ToString
                                End If
             Next
    but Im getting as output:

    System.Windows.Forms.HtmlElement
    I'm also trying with:-
    Code:
     Dim element = WebBrowser1.Document.GetElementById("myImageId44")
             If element IsNot Nothing AndAlso element.Document.Links.Count > 0 Then
                 Dim link = element.Document.Links(0).GetAttribute("href")
             End If
    but I get a different link from what I expect.

    To be clear, the website I'm working is this one


    This is a GIF to show you

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Get href of a button through web browser control

    Try your second code with a different Links index

  3. #3
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,945

    Re: Get href of a button through web browser control

    @.paul:

    When I try the html provided and check the number of links I get exactly one link.

    Code:
       Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
          Dim element = WebBrowser1.Document.GetElementById("myImageId44")
    
          If element IsNot Nothing AndAlso element.Document.Links.Count > 0 Then
             Dim link = element.Document.Links(0).GetAttribute("href")
    
             MessageBox.Show(element.Document.Links.count.ToString)
          End If
       End Sub
    So how is changing the index going to help the op?

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: Get href of a button through web browser control

    The DOM tree is:
    • <a />
      • <button /> #myImageId44
        • <font />


    The href lives as an attribute on #myImageId44's parent DOM node. So what you'll want to do is get the button by the Id, get the parent, and t hen get the href:
    Code:
    Dim buttonElement = WebBrowser1.Document.GetElementById("myImageId44")
    If (buttonElement Is Nothing) Then
        Return
    End If
    
    Dim anchorElement = buttonElement.Parent
    If (anchorElement Is Nothing) Then
        Return
    End If
    
    Dim href = anchorElement.GetAttribute("href")
    If (String.IsNullOrWhitespace(href)) Then
        Return
    End If
    
    MessageBox.Show(href)
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Get href of a button through web browser control

    See whether they have an API, first. That's a converter, so it is pretty likely that they'd offer up an API for doing the same work. If so, use that. Working with the HTML of the page is a recipe for misery. Web developers can't resist tweaking the HTML for the latest and greatest flavor of things. It might be a button today and a <div> tomorrow, or something weirder. The very point of API's is to make an easier interface for programs to work with the mechanism of the site.
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width