从WebBrowserControl点击的HtmlElement获取的XPath [英] Get XPath from clicked HtmlElement in WebBrowserControl

查看:492
本文介绍了从WebBrowserControl点击的HtmlElement获取的XPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能从WebBrowserControl一个点击的HtmlElement中的XPath

How can I get the XPath from a clicked HtmlElement in the WebBrowserControl?

这是我如何获取点击的HtmlElement:

This is how I retrieve the clicked HtmlElement:

System.Windows.Forms.HtmlDocument document = this.webBrowser1.Document;
document.MouseUp += new HtmlElementEventHandler(this.htmlDocument_Click);

private void htmlDocument_Click(object sender, HtmlElementEventArgs e)
{
    HtmlElement element = this.webBrowser1.Document.GetElementFromPoint(e.ClientMousePosition);
}



我想点击特定元素(价格,文章编号,说明等)一个网站,并得到他们的XPath表达式。

I want to click specific elements (price, article number, description, etc) on a website and get their XPath expressions.

感谢您!

推荐答案

XPath表达式不是HTML的标准特性(不像XML)。如果你正在寻找让你可以在以后使用的Html敏捷性包使用元素的XPath,你至少有两个选项:

XPath expression is not a standard feature of HTML (unlike with XML). If you're looking to get an element XPath which you can later use with Html Agility Pack, you have at least two options:


  1. 使用的 HtmlElement.Parent 并构建
    的。XPath的手动

  1. Walk up the element's DOM ancestry tree using HtmlElement.Parent and construct the XPath manually.

使用HTML敏捷性包本身做这样的事情(未经测试):

Use Html Agility Pack itself and do something like this (untested):

HtmlElement element = this.webBrowser1.Document.GetElementFromPoint(e.ClientMousePosition);

var savedId = element.Id;
var uniqueId = Guid.NewGuid().ToString();
element.Id = uniqueId;

var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(element.Document.GetElementsByTagName("html")[0].OuterHtml);
element.Id = savedId;

var node = doc.GetElementbyId(uniqueId);
var xpath = node.XPath;

这篇关于从WebBrowserControl点击的HtmlElement获取的XPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆