Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Href method added to HyperLink. #21

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ScrapySharp/Html/By.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,10 @@ public static By Class(string query, StringComparison comparisonType = StringCom
{
return new By(query, ElementSearchKind.Class, comparisonType);
}

public static By Title(string query, StringComparison comparisonType = StringComparison.CurrentCulture)
{
return new By(query, ElementSearchKind.Title, comparisonType);
}
}
}
12 changes: 9 additions & 3 deletions ScrapySharp/Html/ElementFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public IEnumerable<HtmlNode> FindElements()
switch (searchKind)
{
case ElementSearchKind.Text:
return html.Descendants(tagName).Where(n => string.IsNullOrEmpty(n.InnerText) ? string.IsNullOrEmpty(query) : n.InnerText.Equals(query, comparisonType));
return html.Descendants(tagName).Where(n => string.IsNullOrEmpty(n.InnerText) ? string.IsNullOrEmpty(query) : n.InnerText.Trim().Equals(query, comparisonType));
case ElementSearchKind.Id:
return from n in html.Descendants(tagName)
where string.IsNullOrEmpty(n.Id) ? string.IsNullOrEmpty(query) : n.Id.Equals(query, comparisonType)
where string.IsNullOrEmpty(n.Id) ? string.IsNullOrEmpty(query) : n.Id.Trim().Equals(query, comparisonType)
select n;
case ElementSearchKind.Name:
return from n in html.Descendants(tagName)
let name = n.GetAttributeValue("name", string.Empty)
let name = n.GetAttributeValue("name", string.Empty).Trim()
where string.IsNullOrEmpty(name) ? string.IsNullOrEmpty(query) : name.Equals(query, comparisonType)
select n;
case ElementSearchKind.Class:
Expand All @@ -43,6 +43,12 @@ where string.IsNullOrEmpty(name) ? string.IsNullOrEmpty(query) : name.Equals(que
let names = @class.Split(new []{' '}, StringSplitOptions.RemoveEmptyEntries)
where names.Contains(query)
select n;
case ElementSearchKind.Title:
return from n in html.Descendants(tagName)
let title = n.GetAttributeValue("title", string.Empty)
let fixedTitle = title.Replace(" ", " ").Trim()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will only treat double spaces cases.

where string.IsNullOrEmpty(fixedTitle) ? string.IsNullOrEmpty(query) : fixedTitle.Equals(query, comparisonType)
select n;
default:
return new List<HtmlNode>();
}
Expand Down
3 changes: 2 additions & 1 deletion ScrapySharp/Html/ElementSearchKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public enum ElementSearchKind
Text,
Id,
Name,
Class
Class,
Title
}
}
10 changes: 10 additions & 0 deletions ScrapySharp/Html/Forms/HyperLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ public string Text
get { return node.InnerText; }
}

public string Href
{
get
{
var ret = string.Empty;
if (node.HasAttributes)
ret = node.Attributes["href"].Value;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

risk of Exception when href is not present

return ret;
}
}
public WebPage Click()
{
var href = node.GetAttributeValue("href", string.Empty);
Expand Down