0

我有一个登录门户的 C# 程序,然后需要测试页面上是否存在具有特定 ID 的元素。为了对此进行测试,我从页面中抓取 HTML 并在 HTML 中搜索具有匹配 ID 的元素。

但是,每当我尝试使用此脚本访问 HTML 时,它总是返回门户登录页面的 HTML,而不是通过门户登录后的页面。我可以 100% 确认该程序正在登录门户,但是由于某种原因它仍然返回错误的 HTML。

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
WebClient client = new WebClient();
string html = client.DownloadString(this.currentStepUrl);
doc.LoadHtml(html);
var foo = (from bar in doc.DocumentNode.DescendantNodes()
          where bar.GetAttributeValue("id", null) == expected
          select bar).FirstOrDefault();
if (foo != null)
{
    currentTestCaseResults[0]++;
}
else
{
    currentTestCaseResults[1]++;
}
4

1 回答 1

0

我猜很容易修复:

将除 if 子句之外的所有内容替换为

HtmlElement expectedElement webBrowser2.Document.GetElementById(expected);
于 2013-08-20T17:51:22.907 回答