0

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

<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:

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

How can I get it? Thanks

  • `iElement` is the HTML element. You need its OuterHtml. -- You can just call `GetElementById()`, since you know the ID of this Element. – Jimi Mar 05 '22 at 17:29
  • The outerhtml contains the link that I don't have yet.. outerHTML: `""` `Dim HtmlCollec As HtmlElementCollection HtmlCollec = WebBrowser1.Document.GetElementsByid("myImageId44") For Each iElement As HtmlElement In HtmlCollec If iElement.OuterHtml Then RichTextBox1.Text = iElement.ToString End If Next` – adato adaue Mar 05 '22 at 17:41
  • what should I use instead? – adato adaue Mar 05 '22 at 17:42
  • I've tried also with `Dim Element As HtmlElement = WebBrowser1.Document.GetElementById("myImageId44") If Element.OuterHtml.Contains(">Download MP3 (Bitrate:") Then RichTextBox1.Text = Element.GetAttribute("value") End If` but it doesn't return any output – adato adaue Mar 05 '22 at 17:48
  • You can try `Element.OuterHtml.GetAttribute("href")` – Shrotter Mar 05 '22 at 18:24
  • which codes of mine you are referring to? – adato adaue Mar 05 '22 at 18:27
  • `dim element = [WebBrowser].Document.GetElementById("myImageId44") if element isnot nothing andalso element.Document.Links.Count > 0 then dim link = element.Document.Links(0).GetAttribute("href") end if` -- Or get the OuterHtml and parse the `href` part. Or use HtmlAgilityPack. – Jimi Mar 05 '22 at 18:28
  • @Jimi I'm getting "identifier expected" on [0] – adato adaue Mar 05 '22 at 18:31
  • Right, I wrote it partially in C#. Fixed. – Jimi Mar 05 '22 at 18:31
  • @jimi that code returns a link but that's not the link I'm expected. I've also tried getting part of the outer html and parse the href part with: `Dim element = WebBrowser1.Document.GetElementById("myImageId44") If element.OuterHtml.Contains(">Download MP3 (Bitrate:") Then RichTextBox1.Text = element.GetAttribute("value") End If` but I'm not getting any output – adato adaue Mar 05 '22 at 18:35
  • What Link did you expect? How many Links are there in the `HtmlElement.Links` collection? I based that snippet on what you posted here. That's `https://x638jxfnvmcxbaxc [...]` – Jimi Mar 05 '22 at 18:38
  • I'm not that sure, now for example I re tried your code and it doesn't give me any link.. I'm working on this website [link](https://ytmp3eu.com/en45/) just using a simple link like [link](https://www.youtube.com/watch?v=SJUhlRoBL8M) and getting the link of the button Download – adato adaue Mar 05 '22 at 18:55
  • @Jimi the HtmlElement.Links has just a single link, this one [link]("https://ytmp3eu.com/hu29/") – adato adaue Mar 05 '22 at 20:18
  • Well, then there's a good change that Web Site, as many others do, uses IFrames to updated dynamic content. So you read just the Links of the main Document. Read the notes here: [How to get an HtmlElement value inside Frames/IFrames?](https://stackoverflow.com/a/53218064/7444103). -- Store the `OuterHtml` when you get one or more Links from the HtmlElement you're inspecting. Possibly using a dedicated class object / collections as shown in that example. – Jimi Mar 05 '22 at 21:03
  • Thanks Jimi I'll try to translate it to vb.net. Does the `GetElementById("myImageId44")` still be valid for that code? – adato adaue Mar 05 '22 at 21:11
  • @Jimi `Dim href = WebBrowser1.Document.GetElementById("myImageId44").Parent.GetAttribute("href")` this works ;) – adato adaue Mar 06 '22 at 20:56

0 Answers0