在 WebBrowser 控件中获取 HTML 复选框状态
Get HTML CheckBox state in a WebBrowser control
如何在 WebBrowser 控件中获取 HTML CheckBox 的状态?
我的 class 中有以下变量:
private WebBrowser quickStartGuideContentWebBrowser = new WebBrowser();
我的 class 中也有以下常量:
private const string doNotShowAgainCheckBoxElementId = "do-not-show-again-checkbox";
我的 class;
中也有以下功能
private HtmlElement GetDoNotShowAgainCheckBox()
{
HtmlDocument document = this.quickStartGuideContentWebBrowser.Document;
if (document == null)
{
Debug.WriteLine("GetDoNotShowAgainCheckBox error: (document == null).");
return null;
}
return document.GetElementById(doNotShowAgainCheckBoxElementId);
}
我正在尝试实现一个 IsDoNotShowAgainCheckBoxChecked
方法,如下所示:
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;
}
您只需要使用 HtmlElement.GetAttribute() 方法获取 checked
属性。
根据 CheckBox 的状态,它将是 "True"
或 "False"
(字符串)。
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'");
}
}
GetAttribute()
方法当然可以用于检索 HtmlElement 的任何其他属性,例如它的 className
.
如何在 WebBrowser 控件中获取 HTML CheckBox 的状态?
我的 class 中有以下变量:
private WebBrowser quickStartGuideContentWebBrowser = new WebBrowser();
我的 class 中也有以下常量:
private const string doNotShowAgainCheckBoxElementId = "do-not-show-again-checkbox";
我的 class;
中也有以下功能private HtmlElement GetDoNotShowAgainCheckBox()
{
HtmlDocument document = this.quickStartGuideContentWebBrowser.Document;
if (document == null)
{
Debug.WriteLine("GetDoNotShowAgainCheckBox error: (document == null).");
return null;
}
return document.GetElementById(doNotShowAgainCheckBoxElementId);
}
我正在尝试实现一个 IsDoNotShowAgainCheckBoxChecked
方法,如下所示:
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;
}
您只需要使用 HtmlElement.GetAttribute() 方法获取 checked
属性。
根据 CheckBox 的状态,它将是 "True"
或 "False"
(字符串)。
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'");
}
}
GetAttribute()
方法当然可以用于检索 HtmlElement 的任何其他属性,例如它的 className
.