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&pxhotisnweqwlkdsjkhghewojicwe0guhifkjbh2qqqhsotjpsfiwhvfnshnrewioujgbhuiovshdkjwvcjhfdbwehjkvkjhsdnvmwouipfdnpsesfiwhvfoczq=yloyeevwdfcbpkdsjkhghewojicwe0guhifkjbh2qqqhszjdssfiwhvfeyczrewioujgbhuiovshdkjwvcjhfdbwehjkvkjhsdnvmwouipfdozixsfiwhvfhcimfslzpcikfrvnqldsjkhghewojicwe0guhifkjbh2qqqhsptjesfiwhvfmzsrrewioujgbhuiovshdkjwvcjhfdbwehjkvkjhsdnvmwouipfdyskesfiwhvfcchh&fkdsadxcwaqcscasxcsacxsaxazxcvsdvdslssafdsvx=174147325&dkjfsjksdadsdvcxxcvswscxrewsdf=sdx2&sdwreggllslsdxvc&sadklxcklkvb=qwrdcbkklsdxk&jiijlks=lz9fSJUhlRoBL8M8qh4Ido&uijdsdkdfkgfdsd=85652262237b1335ca34.14415852&lkfdsfxc=lz9fSJUhlRoBL8M8qh4Ido&nqusjmslpeprtmdsjkhghewojicwe0guhifkjbh2qqqhstexrsfiwhvfmmoorewioujgbhuiovshdkjwvcjhfdbwehjkvkjhsdnvmwouipfdwxjwsfiwhvfhlmj=yyhnlsoahifiztdsjkhghewojicwe0guhifkjbh2qqqhscscvsfiwhvffbbsrewioujgbhuiovshdkjwvcjhfdbwehjkvkjhsdnvmwouipfdryansfiwhvfmwdn222811prciwnbztwy5&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:
Quote:
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
Re: Get href of a button through web browser control
Try your second code with a different Links index
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?
Re: Get href of a button through web browser control
The DOM tree is:
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)
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.