在WebBrowser控件中获取子锚点元素 [英] Obtain child anchor element within WebBrowser control

查看:146
本文介绍了在WebBrowser控件中获取子锚点元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

序言

我正在使用 WebBrowser 将与之进行交互,因此解决方案需要使用可见的 WebBrowser 控件。

I'm using the WebBrowser control, which a user will interact with, so a solution will need to work with a visible WebBrowser control.

问题

如何检查元素是否具有锚点儿童?所有浏览器都能够区分一个元素包含一个锚点(< a href =... ),并提供打开新选项卡功能。这就是我试图复制的。但是,当我右键单击 HtmlElement 我只能获取父元素。

How do I check if an element has an anchor as a child? All browsers are able to distinguish that an element contains an anchor (<a href=""...), and offers "open in new tab" functionality. That is what I am attempting to replicate. However, when I right click on a HtmlElement I'm only able to obtain the parent element.

示例

以BBC网站为例,当我右键点击在突出显示的元素(如下图)中,我的输出是 DIV ,但是查看源代码是一个锚点元素作为这个 div

Taking the BBC website as an example, when I right click on the highlighted element (picture below), my output is DIV, but viewing the source code there is an anchor element as a child of this div.

SSCCE

using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace BrowserLinkClick
{
    public partial class Form1 : Form
    {
        private WebBrowser wb;
        private bool firstLoad = true;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            wb = new WebBrowser();
            wb.Dock = DockStyle.Fill;
            Controls.Add(wb);
            wb.Navigate("http://bbc.co.uk");
            wb.DocumentCompleted += wb_DocumentCompleted;
        }

        private void Document_MouseDown(object sender, HtmlElementEventArgs e)
        {
            if (e.MouseButtonsPressed == MouseButtons.Right)
            {
                HtmlElement element = wb.Document.GetElementFromPoint(PointToClient(MousePosition));
                //I assume I need to check if this element has child elements that contain a TagName "A"
                if (element.TagName == "A")
                    Debug.WriteLine("Get link location, open in new tab.");
                else
                    Debug.WriteLine(element.TagName);
            }
        }


        private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (firstLoad)
            {
                wb.Document.MouseDown += new HtmlElementEventHandler(Document_MouseDown);
                firstLoad = false;
            }
        }

    }
}

请使用BBC网站和突出显示的标题测试任何提议的解决方案(标题更改,但DOM保持不变)。

Please test any proposed solution using the BBC website and the highlighted headline (the headline changes, but the DOM remains the same).

推荐答案

您的程序还有其他错误。在BBC网站上,您的代码适用于新闻文章(尽管我看到该网站的非英文版本)。在其他网站上有一些作为子代码的锚点元素,代码如下:

There has to be something else wrong with your program. On the BBC website your code works for the news articles (although I see the non UK version of the site). On other websites where there are anchor elements as children the code below works

 private void Document_MouseDown(object sender, HtmlElementEventArgs e)
    {
        if (e.MouseButtonsPressed == MouseButtons.Right)
        {
            HtmlElement element = wb.Document.GetElementFromPoint(PointToClient(MousePosition));
            if (element.Children.Count > 0)
            {
                foreach (HtmlElement child in element.Children)
                {
                    if (child.TagName == "A")
                        Debug.WriteLine("Get link location, open in new tab.");
                }
            }
            else
            {
                //I assume I need to check if this element has child elements that contain a TagName "A"
                if (element.TagName == "A")
                    Debug.WriteLine("Get link location, open in new tab.");
                else
                    Debug.WriteLine(element.TagName);
            }
        }
    }

这篇关于在WebBrowser控件中获取子锚点元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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