In this string exist number li tag .I want get href attribute of a tags such as this:
http://bipardeh94.blogfa.com" target="_blank
http://avaejam.blogfa.com" target="_blank
and ... I want that do this with C# .How to done this? I use this code but this is not complete .
int indexStartUl = _codeHtml.IndexOf("<ul");
int indexEndUl = _codeHtml.IndexOf("</ul>");
_codeHtml = _codeHtml.Substring(indexStartUl, indexEndUl);
Please help.
<ul class="ull">
<li><a href="http://bipardeh94.blogfa.com" target="_blank">باغ بلور</a><span class="ur">bipardeh94.blogfa.com</span><span class="ds">فرهنگی-خبری-علمی</span></li>
<li><a href="http://avaejam.blogfa.com" target="_blank">هزار نکته </a><span class="ur">avaejam.blogfa.com</span><span class="ds"> يك نكته از هزار نكته باشد تا بعد </span></li>
<li><a href="http://prkangavar.blogfa.com" target="_blank">روابط عمومی دانشگاه آزاداسلامی کنگاور</a><span class="ur">prkangavar.blogfa.com</span><span class="ds">اخبار دانشگاه</span></li>
<li><a href="http://bordekhoun.blogfa.com" target="_blank">وبلاگ اطلاع رسانی بردخون</a><span class="ur">bordekhoun.blogfa.com</span><span class="ds">اخباروگزارشات وتحلیل ها درباره بردخون</span></li>
<li><a href="http://mahinvare.blogfa.com" target="_blank">تدوری های نوین</a><span class="ur">mahinvare.blogfa.com</span><span class="ds">نظریه های علوم انسانی باید متحول شود</span></li>
<li><a href="http://zanjanuniversity.blogfa.com" target="_blank">دانشگاه زنجان</a><span class="ur">zanjanuniversity.blogfa.com</span><span class="ds">اخبار دانشگاهیان زنجان و دانشگاه آزاد زنجان و سیستم ثبت نام شهردای زنجان </span>
</li>
</ul>
If you have a reference to the element, you can get the href from it like this: var link = theElement. href; // or var link = theElement. getAttribute("href");
Answer: Use the jQuery . attr() Method attr() method to dynamically set or change the value of href attribute of a link or anchor tag. This method can also be used to get the value of any attribute.
Attributes should always be applied with start tag. The Attribute should always be applied with its name and value pair.
You can use Selenium
WebDriver
functionality:
IList<IWebElement> lis = driver.FindElements(By.CssSelector(".ull > li"));
foreach (IWebElement li in lis) {
string href = li.GetAttribute("href");
}
You find all WebElements
with li
tag who are children of WebElement
with class ull
, and the iterate over the list and take the href attribute.
You can use Html Agility Pack
Html Agility Pack Examples:
HtmlDocument doc = new HtmlDocument();
doc.Load("file.htm");
foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href"])
{
HtmlAttribute att = link["href"];
att.Value = FixLink(att);
}
doc.Save("file.htm");
Links :
How to use HTML Agility pack
http://www.mikesdotnetting.com/article/273/using-the-htmlagilitypack-to-parse-html-in-asp-net http://www.codeproject.com/Articles/691119/Html-Agility-Pack-Massive-information-extraction-f
I hope this information will help
For better understanding
Substring(a,b)
In your ex you take:
a as start index of ul
b as end index of ul // Error b will be the length from string start to end of ul!
you need to do is:
int c = b - a // (will give you the inner text length)
_codeHtml = _codeHtml.Substring(a,c);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With