Stavo creando un'applicazione che inserisce automaticamente i dati nei tag di input html. Ho xPath per tag specifici come '/ html / body / form / div / div [2] / div / div / input' e sono riuscito a ottenere HtmlNode con l'aiuto di HtmlAgilityPack
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);
}
Ora ho bisogno di selezionare in qualche modo HtmlElement da Webbrowser.Document che corrisponde all'attuale HtmlNode. Qualcuno può aiutarmi con quello?
BTW: Non sto creando alcun bot di spamming.
Ciao a tutti di nuovo. Ho trovato una soluzione con ricorsione, un sacco di istruzioni if e nessun pacchetto htmlagility, ma sfortunatamente non riesco a postarlo subito. Sembra che non abbia abbastanza reputazione.
Tuttavia, se non fa troppo sforzo, puoi dirmi come risolvere questo problema con htmlagilitypack, perché il mio codice sembra davvero brutto.
Grazie a tutti. Dopo aver pensato e programmato per quasi un giorno intero, sono giunto alla decisione di dover utilizzare htmlElement nativo invece di htmlagilitypack HtmlNode, perché voglio inserire il testo in Htmlelement in webbrowser. quindi ecco il codice che mi è venuto in mente. comunque mi piacerebbe che qualcuno mostrasse una soluzione con 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;
}
la funzione è chiamata in questo modo. dove htmlController è l'istanza di qualche classe.
HtmlElement currentElement = htmlController.selectHtmlNode("/body/form/div/div[2]/div/div/input", webBrowser.Document.GetElementsByTagName("html")[0]);
currentElement.SetAttribute("Value", "hello world");