我使用WebBrowser
控制,其用戶將與互動,所以解決方案將需要用可見WebBrowser
控制工作。
問題
如何檢查元素是否有一個錨作爲一個孩子?所有瀏覽器都可以區分一個元素包含錨點(<a href=""...
),並提供「在新選項卡中打開」功能。這正是我試圖複製的內容。但是,當我右鍵點擊一個HtmlElement
我只能夠獲得父元素。
例
以BBC網站作爲一個例子,當我右擊突出顯示的元素上(下圖),我的輸出是DIV
,但觀察源代碼有一個錨元件作爲子這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保持不變)。
不幸的是,這仍然沒有返回我的例子'a'標籤。你可以嘗試從我的原始帖子的BBC例子,看看是否是你的情況?新聞項目已更改,但DOM結構保持不變。 – TEK