¿Cómo puedo obtener el estado de un CheckBox HTML en un control WebBrowser?
Tengo la siguiente variable en mi clase:
private WebBrowser quickStartGuideContentWebBrowser = new WebBrowser();También tengo la siguiente constante en mi clase:
private const string doNotShowAgainCheckBoxElementId = "do-not-show-again-checkbox";También tengo la siguiente función en mi clase;
private HtmlElement GetDoNotShowAgainCheckBox() { HtmlDocument document = this.quickStartGuideContentWebBrowser.Document; if (document == null) { Debug.WriteLine("GetDoNotShowAgainCheckBox error: (document == null)."); return null; } return document.GetElementById(doNotShowAgainCheckBoxElementId); } Estoy tratando de implementar un método IsDoNotShowAgainCheckBoxChecked de la siguiente manera:
private bool IsDoNotShowAgainCheckBoxChecked() { HtmlElement checkboxElement = GetDoNotShowAgainCheckBox(); if (checkboxElement == null) { Debug.WriteLine("IsDoNotShowAgainCheckBoxChecked error: (checkboxElement == null)."); return false; } // What magical incantation do I put here // to retrieve the checked state of checkboxElement as a bool? return false; }Solo necesita obtener el atributo checked usando el método HtmlElement.GetAttribute() .
Será "True" o "False" (cadena), según el estado del CheckBox.
if (quickStartGuideContentWebBrowser.ReadyState != WebBrowserReadyState.Complete) return; var chkBoxElm = quickStartGuideContentWebBrowser.Document. GetElementById(doNotShowAgainCheckBoxElementId); if (chkBoxElm != null) { var state = chkBoxElm.GetAttribute("checked"); if (state = "True") { Console.WriteLine("The CheckBox state is 'checked'"); } } Por supuesto, el método GetAttribute() puede usarse para recuperar cualquier otro atributo de un HtmlElement , por ejemplo, su className .