- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在创建一个自动将数据插入 html 输入标签的应用程序。我有用于特定标记的 xPath,例如“/html/body/form/div/div[2]/div/div/input”,我在 HtmlAgilityPack 的帮助下设法获得了 HtmlNode
var documentAsIHtmlDocument3 = (mshtml.IHTMLDocument3)webBrowser.Document.DomDocument;
StringReader sr = new StringReader(documentAsIHtmlDocument3.documentElement.outerHTML);
htmlDocument.Load(sr);
if (htmlDocument.DocumentNode != null)
{
HtmlNode currentNode = htmlDocument.DocumentNode.SelectSingleNode(xPath);
}
现在我需要以某种方式从对应于当前 HtmlNode 的 Webbrowser.Document 中选择 HtmlElement 。有人可以帮我吗?
顺便说一句:我没有创建任何垃圾邮件机器人。
大家好。我找到了递归的解决方案,很多 if 语句但没有 htmlagilitypack,但不幸的是我现在不能发布它。看来我的声望不够。
不过,如果不太费力的话,你能告诉我如何用htmlagilitypack解决这个问题吗,因为我的代码看起来真的很讨厌。
最佳答案
谢谢大家。经过几乎一整天的思考和编程,我决定必须使用原生 htmlElement 而不是 htmlagilitypack HtmlNode,因为我想在 webbrowser 中将文本输入到 Htmlelement 中。所以这是我想出的代码。如果有人用 htmlagilitypack 展示解决方案,我仍然会很感激。
public HtmlElement selectHtmlNode(string xPath, HtmlElement htmlElement)
{
string currentNode;
int indexOfElement;
//get string representation of current Tag.
if (xPath.Substring(1,xPath.Length-2).Contains('/'))
currentNode = xPath.Substring(1, xPath.IndexOf('/', 1) - 1);
else
currentNode = xPath.Substring(1, xPath.Length-1);
//gets the depth of current xPath
int numOfOccurence = Regex.Matches(xPath, "/").Count;
//gets the children's index
int.TryParse(Regex.Match(currentNode, @"\d+").Value, out indexOfElement);
//if i have to select nth-child ex: /tr[4]
if (indexOfElement > 1)
{
currentNode = currentNode.Substring(0, xPath.IndexOf('[') - 1);
//the tag that i want to get
if (numOfOccurence == 1 || numOfOccurence == 0)
{
return htmlElement.Children[indexOfElement - 1];
}
//still has some children tags
if (numOfOccurence > 1)
{
int i = 1;
//select nth-child
foreach (HtmlElement tempElement in htmlElement.Children)
{
if (tempElement.TagName.ToLower() == currentNode && i == indexOfElement)
{
return selectHtmlNode(xPath.Substring(xPath.IndexOf('/', 1)), tempElement);
}
else if (tempElement.TagName.ToLower() == currentNode && i < indexOfElement)
{
i++;
}
}
}
}
else
{
if (numOfOccurence == 1 || numOfOccurence == 0)
{
return htmlElement.FirstChild;
}
if (numOfOccurence > 1)
{
foreach (HtmlElement tempElement in htmlElement.Children)
{
if (tempElement.TagName.ToLower() == currentNode)
{
return selectHtmlNode(xPath.Substring(xPath.IndexOf('/', 1)), tempElement);
}
}
}
}
return null;
}
函数就是这样调用的。其中 htmlController 是某个类的实例。
HtmlElement currentElement = htmlController.selectHtmlNode("/body/form/div/div[2]/div/div/input", webBrowser.Document.GetElementsByTagName("html")[0]);
currentElement.SetAttribute("Value", "hello world");
关于c# - 如何将 HtmlAgilityPack 的 Htmlnode 转换为 webbrowser HtmlElement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10969393/
我应该如何添加到我的项目 HTMLParser、htmlnode、...?因为添加(.h和.m)但是我得到如下错误: 找不到 libxml/HTMLparser.h 文件! 发送。 最佳答案 您必须在
我正在使用 HTML Agility Pack 来解析和转换 HTML 文件,但由于索引参数,在尝试创建新的 HTMLNode 时出现异常“项目已添加”。 HtmlNode node1 = new H
我正在尝试使用 HtmlAgilityPack 来查找网站中的元素。我的问题如下: 我创建了一个 Windows 8 通用应用程序 (c#) 使用我添加的 NuGet 管理器: using Syste
我使用 HtmlAgilityPack 来解析 webbrowser 控件的 html 文档。我能够找到我想要的 HtmlNode,但是在获得 HtmlNode 之后,我想在 WebbrowserCo
如何知道 HtmlNode我正在使用的是 ?我知道 NodeType是一个 Element但是你如何确定它是否是 如果您需要更多信息,请告诉我。 最佳答案 你可以这样检查,只需要使用Name属性:
我是 JavaScript 新手,当我使用 DOM 模型时,当我想要显示 HTML childNodes 中的所有 nodeName 时,我感到很困惑。当我有一个 head 标签并且删除完整的 hea
上下文: 我正在解析关于 this service 的查询结果,但结果的 HTML 是一团糟。 我的目标是构建一个“KeyValue”对,每个“属性和值”都显示为该查询的结果。 目前我脑子里只有一种方
我正在尝试为给定的行/字符位置(例如第 5 行,第 12 个字符)找到相应的 HtmlNode。我查看了帮助文档,但不太确定它是否可用。 这可以在 Html Agility Pack 中实现吗? 编辑
我正在创建一个自动将数据插入 html 输入标签的应用程序。我有用于特定标记的 xPath,例如“/html/body/form/div/div[2]/div/div/input”,我在 HtmlAg
如果“SelectNodes”返回 NULL,我如何在下面的 foreach 循环中捕获 NullReferenceException 错误? 我在 stackoverflow 上进行了搜索,发现提到
我使用默认的 .net WebBrowser 创建了一个 HTMLElement picker (DOM)。 用户可以通过单击选择(选择)一个 HTMLElement。 我想获取HTMLElement
我花了很长时间试图解决这个问题。任何人都可以帮我解决这个问题。 我完全按照这个 youtube 教程进行操作: http://www.youtube.com/watch?v=3Huh44nsZTw 当
我是一名优秀的程序员,十分优秀!