10

I am trying to code a way using webBrowser1 to get a hold of of a download link via href, but the problem is I must find it using its class name.

<body>
<iframe scrolling="no" frameborder="0" allowtransparency="true" tabindex="0" name="twttrHubFrame" style="position: absolute; top: -9999em; width: 10px; height: 10px;" src="http://platform.twitter.com/widgets/hub.html">
‌¶
<div id="main">
‌¶‌→
<div id="header">
<div style="float:left;">
‌¶‌→
<div id="content">
‌¶‌→
<h1 style="background-image:url('http://static.mp3skull.com/img/bgmen.JPG'); background-repeat:repeat-x;">Rush‌·Mp3‌·Download</h1>
‌¶‌→
<a id="bitrate" onclick="document.getElementById('ofrm').submit(); return false;" rel="nofollow" href="">
<form id="ofrm" method="POST" action="">
‌¶‌→‌¶‌→‌→
<div id="song_html" class="show1">
‌¶‌→‌→‌→
<div class="left">
‌¶‌→‌→‌→
<div id="right_song">
‌¶‌→‌→‌→‌→
<div style="font-size:15px;">
‌¶‌→‌→‌→‌→
<div style="clear:both;"></div>
‌¶‌→‌→‌→‌→
<div style="float:left;">
‌¶‌→‌→‌→‌→‌→
<div style="float:left; height:27px; font-size:13px; padding-top:2px;">
‌¶‌→‌→‌→‌→‌→‌→
<div style="float:left; width:27px; text-align:center;">
‌¶‌→‌→‌→‌→‌→‌→
<div style="margin-left:8px; float:left;">
<a style="color:green;" target="_blank" rel="nofollow" href="http://dc182.4shared.com/img/1011303409/865387c9/dlink__2Fdownload_2F6QmedN8H_3Ftsid_3D20111211-54337-a79f8d10/preview.mp3">Download</a>
</div>
‌·‌¶‌→‌→‌→‌→‌→‌→
<div style="margin-left:8px; float:left;">
‌¶‌→‌→‌→‌→‌→‌→
<div style="margin-left:8px; float:left;">
‌·‌¶‌→‌→‌→‌→‌→‌→
<div style="clear:both;"></div>
‌¶‌→‌→‌→‌→‌→
</div>
‌¶‌→‌→‌→‌→‌→
<div id="player155580779" class="player" style="float:left; margin-left:10px;"></div>
‌¶‌→‌→‌→‌→
</div>
‌→‌¶‌→‌→‌→‌→
<div style="clear:both;"></div>
‌¶‌→‌→‌→
</div>
‌¶‌→‌→‌→
<div style="clear:both;"></div>
‌¶‌→‌→
</div>

I looked and searched all over google, but I found PHP examples?

I understand you would do something along the lines of this

HtmlElement downloadlink = webBrowser1.Document.GetElementById("song_html").All[0];
URL = downloadlink.GetAttribute("href");

but I do not understand how to do it by the class "show1".

Please point me in the right direction with examples and/or a website I can visit so I can learn how to do this as I searched and have no clue.

EDIT: I pretty much need the href link ("http://dc182.4shared.com/img/1011303409/865387c9/dlink__2Fdownload_2F6QmedN8H_3Ftsid_3D20111211-54337-a79f8d10/preview.mp3"), so how would I obtain it?

Andrew
  • 552
  • 2
  • 10
  • 29
  • Where do "show1" and "webBrowser1" come from? If you're using example code or a library please include that information. – Matthew Read Dec 11 '11 at 04:20
  • show1 is the class name in the HTML code I posted above. webBrowser1 is simply what I am using to access a web page via the form. – Andrew Dec 11 '11 at 04:21

3 Answers3

17

There is nothing built-in in the WebBrowser control to retrieve an element by class name. Since you know it is going to be an a element the best you can do is get all a elements and search for the one you want:

var links = webBrowser1.Document.GetElementsByTagName("a");
foreach (HtmlElement link in links)
{
    if (link.GetAttribute("className") == "show1")
    {
        //do something
    }
}
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • So you are saying with this code, it will find the classname "show1" and from there I can retrieve the URL by then using getattribute of href? – Andrew Dec 11 '11 at 04:32
  • Yes - it will find all `a` elements with a a class name of "show1", then you can retrieve the link as before with `link.GetAttribute("href")` – BrokenGlass Dec 11 '11 at 04:34
  • For some reason it won't retrieve the URL? Would there be any reasons to this? After looking through the HTML, it is all under an id called 'content' so would I have to search that first?
    I tried something like; var links = webBrowser1.Document.GetElementById("content").Document.GetElementsByTagName("a"); but it is giving me an error
    – Andrew Dec 11 '11 at 04:41
  • can you update your question with the relevant html that contains your link? – BrokenGlass Dec 11 '11 at 04:47
  • @BrokenGlass It should be _classname_ and not class. https://social.msdn.microsoft.com/Forums/vstudio/en-US/a22cafb7-f93c-4911-91ce-b305a54811fa/how-to-get-element-by-class-in-c – TechGeek Apr 28 '15 at 11:54
3

Extension Method for HtmlDocument

Returns a list of elements with a particular tag, which coincides with the given className

It can be used to capture the elements only on the tag, or only by class name

internal static class Utils
{
  internal static List<HtmlElement> getElementsByTagAndClassName(this HtmlDocument doc, string tag = "", string className = "")
  {
      List<HtmlElement> lst = new List<HtmlElement>();
      bool empty_tag = String.IsNullOrEmpty(tag);
      bool empty_cn = String.IsNullOrEmpty(className);
      if (empty_tag && empty_cn) return lst;
      HtmlElementCollection elmts = empty_tag ? doc.All : doc.GetElementsByTagName(tag);
      if (empty_cn)
      {
         lst.AddRange(elmts.Cast<HtmlElement>());
         return lst;
      }
      for (int i = 0; i < elmts.Count; i++)
      {
         if (elmts[i].GetAttribute("className") == className)
         {
            lst.Add(elmts[i]);
         }
      }
      return lst;
   }
}

Usage:

WebBrowser wb = new WebBrowser();
List<HtmlElement> lst_div = wb.Document.getElementsByTagAndClassName("div");// all div elements
List<HtmlElement> lst_err_elmnts = wb.Document.getElementsByTagAndClassName(String.Empty, "error"); // all elements with "error" class
List<HtmlElement> lst_div_err = wb.Document.getElementsByTagAndClassName("div", "error"); // all div's with "error" class
Ramil Shavaleev
  • 364
  • 3
  • 12
2

I followed up these answers and make my method to hide div by class name.

I shared for whom concern.

public void HideDivByClassName(WebBrowser browser, string classname)
        {
            if (browser.Document != null)
            {
                var byTagName = browser.Document.GetElementsByTagName("div");
                foreach (HtmlElement element in byTagName)
                {
                    if (element.GetAttribute("className") == classname)
                    {
                        element.Style = "display:none";
                    }
                }
            }
        }
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62