html 입력 태그에 데이터를 자동으로 삽입하는 응용 프로그램을 만들었습니다. '/ html / body / form / div / div [2] / div / div / input'과 같은 특정 태그에 대한 xPath를 가지고 있으며 HtmlAgilityPack의 도움으로 HtmlNode를 얻을 수있었습니다.
var documentAsIHtmlDocument3 = (mshtml.IHTMLDocument3)webBrowser.Document.DomDocument;
StringReader sr = new StringReader(documentAsIHtmlDocument3.documentElement.outerHTML);
htmlDocument.Load(sr);
if (htmlDocument.DocumentNode != null)
{
HtmlNode currentNode = htmlDocument.DocumentNode.SelectSingleNode(xPath);
}
이제 어떻게 든 현재 HtmlNode에 해당하는 Webbrowser.Document에서 HtmlElement를 선택해야합니다. 누군가가 저를 도울 수 있습니까?
BTW : 나는 스패밍 봇을 만들지 않을 것이다.
안녕하세요. 재귀, 많은 if 문과 htmlagilitypack이없는 솔루션을 찾았지만 불행히도 지금은 게시 할 수 없습니다. 나는 충분한 명성이없는 것 같다.
그래도 너무 많은 노력을하지 않는다면, htmlagilitypack으로이 문제를 해결하는 방법을 알려주시겠습니까? 제 코드가 정말 불쾌 해 보입니다.
모두 감사합니다. 거의 하루 종일 생각하고 프로그래밍 한 후에 나는 웹 브라우저에서 텍스트를 Htmlelement에 입력하려고하기 때문에 htmlNility 대신 htmlagility htmlElement를 사용해야한다는 결정을 내 렸습니다. 그래서 여기에 내가 생각해 낸 코드가 있습니다. 누군가가 htmlagilitypack 솔루션을 보여 주면 여전히 감사하겠습니다.
public HtmlElement selectHtmlNode(string xPath, HtmlElement htmlElement)
{
string currentNode;
int indexOfElement;
//get string representation of current Tag.
if (xPath.Substring(1,xPath.Length-2).Contains('/'))
currentNode = xPath.Substring(1, xPath.IndexOf('/', 1) - 1);
else
currentNode = xPath.Substring(1, xPath.Length-1);
//gets the depth of current xPath
int numOfOccurence = Regex.Matches(xPath, "/").Count;
//gets the children's index
int.TryParse(Regex.Match(currentNode, @"\d+").Value, out indexOfElement);
//if i have to select nth-child ex: /tr[4]
if (indexOfElement > 1)
{
currentNode = currentNode.Substring(0, xPath.IndexOf('[') - 1);
//the tag that i want to get
if (numOfOccurence == 1 || numOfOccurence == 0)
{
return htmlElement.Children[indexOfElement - 1];
}
//still has some children tags
if (numOfOccurence > 1)
{
int i = 1;
//select nth-child
foreach (HtmlElement tempElement in htmlElement.Children)
{
if (tempElement.TagName.ToLower() == currentNode && i == indexOfElement)
{
return selectHtmlNode(xPath.Substring(xPath.IndexOf('/', 1)), tempElement);
}
else if (tempElement.TagName.ToLower() == currentNode && i < indexOfElement)
{
i++;
}
}
}
}
else
{
if (numOfOccurence == 1 || numOfOccurence == 0)
{
return htmlElement.FirstChild;
}
if (numOfOccurence > 1)
{
foreach (HtmlElement tempElement in htmlElement.Children)
{
if (tempElement.TagName.ToLower() == currentNode)
{
return selectHtmlNode(xPath.Substring(xPath.IndexOf('/', 1)), tempElement);
}
}
}
}
return null;
}
함수가 이런 식으로 호출됩니다. 여기서 htmlController는 어떤 클래스의 인스턴스입니다.
HtmlElement currentElement = htmlController.selectHtmlNode("/body/form/div/div[2]/div/div/input", webBrowser.Document.GetElementsByTagName("html")[0]);
currentElement.SetAttribute("Value", "hello world");