Reputation: 187
How can I verify if a checkbox is checked or unchecked using Selenium?
Because method "element.Selected" does not work in this case.
Here is the HTML code if a checkbox is checked:
<td class="dxgvCommandColumn_MetropolisBlue dxgv" onclick="aspxGVScheduleCommand('messageGrid',['Select',1],1)" align="center">
<span id="messageGrid_DXSelBtn1_D" class="dxICheckBox_MetropolisBlue dxichSys dxWeb_edtCheckBoxChecked_MetropolisBlue">
<input id="messageGrid_DXSelBtn1" value="U" readonly="readonly" style="border-width:0;width:0;height:0;padding:0;margin:0;position:relative;background-color:transparent;display:block;" type="text"/>
</span>
</td>
Here is the HTML code if a checkbox is unchecked:
<td class="dxgvCommandColumn_MetropolisBlue dxgv" onclick="aspxGVScheduleCommand('messageGrid',['Select',1],1)" align="center">
<span id="messageGrid_DXSelBtn1_D" class="dxWeb_edtCheckBoxUnchecked_MetropolisBlue dxICheckBox_MetropolisBlue dxichSys">
<input id="messageGrid_DXSelBtn1" value="U" readonly="readonly" style="border-width:0;width:0;height:0;padding:0;margin:0;position:relative;background-color:transparent;display:block;" type="text"/>
</span>
</td>
Image:
Upvotes: 1
Views: 10474
Reputation: 335
"element.Selected" is not working, because you do not have checkbox element on the page at all. Selenium does not know that the input "looks" like checkbox. It is just HTML input of type: "text".
Your only chance is to get the JavaScript executor and check the attributes yourself (elm is your Selenium "input" object got by XPath or CSS selector):
ExecuteScript("arguments[0].setAttribute(arguments[1], arguments[2]);",
new object[] {elm, "value", value });
ExecuteScriptReturn("return arguments[0].getAttribute(arguments[1]);",
new object[] {elm, "value" });
where the method is:
private void ExecuteScript(string script, object[] arguments)
{
var jsExecutor = (IJavaScriptExecutor)_browser;
jsExecutor.ExecuteScript(script, arguments);
}
private void ExecuteScriptReturn(string script, object[] arguments)
{
var jsExecutor = (IJavaScriptExecutor)_browser;
return (string)jsExecutor.ExecuteScript(script, arguments);
}
EDIT:
Just note you in fact are not checking the value of input in this case, but the value of class of these two elements:
span id="messageGrid_DXSelBtn1_D" class="dxICheckBox_MetropolisBlue dxichSys dxWeb_edtCheckBox**Checked**_MetropolisBlue"
and
span id="messageGrid_DXSelBtn1_D" class="dxWeb_edtCheckBox**Unchecked**_MetropolisBlue dxICheckBox_MetropolisBlue dxichSys"
Upvotes: 1
Reputation: 193088
As per the HTML you have shared, it's clear that the <span>
tag of the outerHTML of the Checkbox contains the following class attributes:
Checked
<span id="messageGrid_DXSelBtn1_D" class="dxICheckBox_MetropolisBlue dxichSys dxWeb_edtCheckBoxChecked_MetropolisBlue">
<input id="messageGrid_DXSelBtn1" value="U" readonly="readonly" style="border-width:0;width:0;height:0;padding:0;margin:0;position:relative;background-color:transparent;display:block;" type="text"/>
</span>
Unchecked
<span id="messageGrid_DXSelBtn1_D" class="dxWeb_edtCheckBoxUnchecked_MetropolisBlue dxICheckBox_MetropolisBlue dxichSys">
<input id="messageGrid_DXSelBtn1" value="U" readonly="readonly" style="border-width:0;width:0;height:0;padding:0;margin:0;position:relative;background-color:transparent;display:block;" type="text"/>
</span>
So to check if the Check Box is checked or unchecked you can induce the following validation:
if(driver.FindElement(By.XPath("//input[@id='messageGrid_DXSelBtn1']//preceding::span[1]")).GetAttribute("class").contains("dxWeb_edtCheckBoxChecked_MetropolisBlue"))
Console.WriteLine("Check Box is Checked");
else
Console.WriteLine("Check Box is Unchecked");
Upvotes: 2