How to get HTML element coordinates using C#?

≯℡__Kan透↙ 提交于 2019-12-08 04:36:12

问题


I am planning to develop web crawler, which would extract coordinates of html elements from web pages. I have found out that it is possible to get html element coordinates by using "mshtml" assembly. Right now I would like to know if it is possible and how to get only necessary information (html,css) from web page, and then by using appropriate mshtml classes get correct coordinates of all html elements?

Thank you!


回答1:


I use these c# functions to determine element positions. You need to pass in a reference to the HTML element in question.

public static int findPosX( mshtml.IHTMLElement obj ) 
{
  int curleft = 0;
  if (obj.offsetParent != null ) 
  {
    while (obj.offsetParent != null ) 
    {
      curleft += obj.offsetLeft;
      obj = obj.offsetParent;
    }
  } 

  return curleft;
}

public static int findPosY( mshtml.IHTMLElement obj ) 
{
  int curtop = 0;
  if (obj.offsetParent != null ) 
  {
    while (obj.offsetParent != null ) 
    {
      curtop += obj.offsetTop;
      obj = obj.offsetParent;
    }
  } 

  return curtop;
}

I get HTML elements from the current document like so:

// start an instance of IE
public SHDocVw.InternetExplorerClass ie;
ie = new SHDocVw.InternetExplorerClass();
ie.Visible = true;

// Load a url
Object Flags = null, TargetFrameName = null, PostData = null, Headers = null;
ie.Navigate( url, ref Flags, ref TargetFrameName, ref PostData, ref Headers );

while( ie.Busy )
{
  Thread.Sleep( 500 );
}

// get an element from the loaded document
mshtml.HTMLDocumentClass document = ((mshtml.HTMLDocumentClass)ie.Document);
document.getElementById("myelementsid");



回答2:


I am not sure how one could do this in C# as it's not my language of choice but it could be done using Javascript, particularly using jQuery's offSet() function.



来源:https://stackoverflow.com/questions/1547614/how-to-get-html-element-coordinates-using-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!