【问题标题】:Using GetElementsById to search a website使用 GetElementsById 搜索网站
【发布时间】:2013-10-21 16:32:03
【问题描述】:
string entry = Titleentry.Text;

HtmlElementCollection theElementCollection;
HtmlDocument filters = webBrowser1.Document;

theElementCollection = webBrowser1.Document.GetElementsByTagName("input");

foreach (HtmlElement curElement in theElementCollection)
{
    if ((curElement.GetAttribute("id").ToString() == "searchTitle"))
    {
        curElement.SetAttribute("value", entry);
    }
}

filters.GetElementById("filterSortBy").GetAttribute("option").Select("price_low_high");

theElementCollection = webBrowser1.Document.GetElementsByTagName("button");

foreach (HtmlElement curElement in theElementCollection)
{
    if (curElement.GetAttribute("id").Equals("searchSubmit"))
    {
        curElement.InvokeMember("click");
    }
}

标记的语句给了我以下信息:

错误 1 ​​无法从用法中推断方法“System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable, System.Func)”的类型参数。尝试明确指定类型参数。

我想要做的是从网站上按 ID 获取元素,因为如果我按标签名称尝试元素,它会选择不正确的元素

我尝试使用 html 集合,但它说不能将 html 元素隐式转换为 html 集合

【问题讨论】:

  • 有人愿意帮助我吗?

标签: c# html linq filter


【解决方案1】:

首先,Select 需要一个Func<TSource, TResult>,而不仅仅是一个表示你想要什么的字符串。其次,您为什么首先尝试使用SelectGetAttribute 返回属性值的字符串。只需声明一个字符串即可捕获该值。

string option = filters.GetElementById("filterSortBy").GetAttribute("option");

【讨论】:

  • 我使用 Select 是因为它是一个选项列表,而且由于我是 c# 中的新手,所以我认为这是自选择以来的常识。使用后如何选择价格从低到高的期权价值?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-25
相关资源
最近更新 更多