請教一個邏輯問題:使用webbrowser控制元件,需要區分一個元素是否為iframe中的元素
我的辦法是:
1、同時訂閱 webBrowser.Document、webBrowser.Document.Window.Frames.Document 中的 MouseOver 事件
2、在 MouseOver 事件中,要區分滑鼠指標當前的元素 HtmlElementEventArgs.ToElement 是屬于哪個 iframe 的元素
3、本身就有一個技術困難:跨域訪問 iframe,我已解決
public static class FrameUtility
{
public static bool CheckContainFrame(this HtmlDocument document, HtmlDocument frameDocument)
{
foreach (HtmlWindow htmlWindow in document.Window.Frames)
{
if (htmlWindow.GetDocument() == frameDocument)
{
return true;
}
}
return false;
}
public static string BuildFrameXPathPrefix(this HtmlDocument document, HtmlDocument frameDocument)
{
int index = 0;
foreach (HtmlWindow htmlWindow in document.Window.Frames)
{
index++;
if (htmlWindow.GetDocument() == frameDocument)
{
break;
}
}
return string.Format("frame[{0}]#", index);
}
}
if (webBrowser.Document.CheckContainFrame(document))
{
//
// 當前檔案為iframe,需要加上前綴
//
txtElementXPath.Text = string.Format("{0}{1}", webBrowser.Document.BuildFrameXPathPrefix(document), xpath);
}
else
{
//
// 保存 XPath
//
txtElementXPath.Text = xpath;
}
現在的邏輯問題是:
1、同時訂閱了 webBrowser.Document、webBrowser.Document.Window.Frames.Document 中的 MouseOver 事件
2、由于 MouseOver 事件會被執行兩次,無法判斷 document 到底是否為 iframe 檔案,還是主檔案
3、iframe有可能是區域一小塊,也有可能是充滿整個頁面(當為 frameset 時)
uj5u.com熱心網友回復:
webBrowser是服務器端控制元件,要判斷iframe只能用注入的js來判斷了轉載請註明出處,本文鏈接:https://www.uj5u.com/net/165480.html
標籤:C#
下一篇:tfs域名連接慢
