이 C# 함수를 사용하여 요소 위치를 결정합니다. 문제의 HTML 요소에 대한 참조를 전달해야합니다.
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;
}
나는과 같이 현재 문서에서 HTML 요소를 얻을 :
// 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");
당신은 브라우저 창 위쪽 및 왼쪽 가장자리에서 픽셀 단위로 좌표를 의미합니까? 이것은 브라우저마다 약간 다르게 렌더링되기 때문에 가능하다고 생각합니다. (그리고 또한 어느 좌표가 "올바른지"를 정의하는 것이 다소 불가능한 것처럼 보일 것입니다.) –
약간의 differnet을 렌더링 할 수도 있지만 한 가지 방법 만 정확합니다 (브라우저 기본값을 제거하는 CSS 파일이 있다고 가정). .) – erikkallen
좌표는 픽셀 단위로 부모 노드와 관련이있을 수 있습니다. 올바른 좌표에 대한 의미는 CSS 파일없이 렌더링 된 HTML 파일이 요소의 다른 좌표를 제공한다는 것입니다. HTMLDocument 클래스를 사용하여 수동으로 다운로드하지 않고 웹 페이지를 분석하고 가능한 한 효율적으로 수행 할 수 있는지 여부는 알 수 없습니다. – spacemonkey