Estoy usando Html Agility Pack para ver si existen divs con una clase e id específicos.
string target = "http://192.168.3.230/index.htm";
WebClient client = new WebClient();
string html = client.DownloadString(target);
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
if (doc.DocumentNode.SelectSingleNode("//div[@id='bit0' and @class='rledoff']") != null){
//actions in here
}
else if (doc.DocumentNode.SelectSingleNode("//div[@id='bit0' and @class='rledon']") != null)
{
//actions in here
}
Actualmente el else si debería ser cierto, pero está realizando las acciones en la sentencia if. He visto este trabajo correctamente cuando lo hice
doc.Load("c:\\somelocaldest\\page.htm");
Cuando trato de hacerlo desde el sitio real en lugar de un archivo guardado localmente del sitio, se verá que la identificación es correcta y no se tiene en cuenta la clase. ¿Qué podría causar que se comporte de manera diferente entre obtener el html para un archivo local y desde un sitio / dispositivo externo?
En primer lugar, le sugiero que haga esto, para mí, funcionó más rápido que WebClient.
HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load(target);
Si dice que esto funciona en el archivo hmtl local y no funciona cuando el html está en el servidor, haga lo siguiente.
File.WriteAllText(path,doc.DocumentNode.OuterHtml);
A veces, el código fuente de la página es diferente cuando lo descarga con htmlweb (o webclient), así que vuelva a hacer su xpath utilizando este nuevo archivo html.