Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

c# - How to getelement by class?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...