Reputation: 137
There is a check box which is displaying as checked already, now when I inspect it shows with image src. in HTML. When I click on the checkbox, it is getting unchecked or checked.
To verify its state, I have written this code, which always brings false even though the checkbox is selected.
WebElement chBox = driver.findElement(By.xpath
("/html/body/div[3]/div[2]/form/fieldset/div[1]/table/tbody/tr[10]/td/img"));
if (chBox.isSelected()) {
System.out.println("User active check box is already checked");
} else
System.out.println("User active check box is not checked");
}
Why?
Upvotes: 6
Views: 28584
Reputation: 1
On python using image of element to find if it is checked.
import PIL
import io
chBox= driver.find_elements(by=By.XPATH,value="/html/body/div[3]/div[2]/form/fieldset/div[1]/table/tbody/tr[10]/td/img")#find checkbox
image_binary = chBox .screenshot_as_png#get raw image of checkbox
img = PIL.Image.open(io.BytesIO(image_binary))#get pil image from raw image
colors=img.getcolors()#list pixels count an color
for color in colors:
if color[1]==(40, 97, 205, 255):#find blue
print("checkbox blue")
break
Upvotes: 0
Reputation: 1
When isselect()
Always Returns False
do this
WebElement chkbox = sc.findElement(By.xpath("(//*[name()='circle'])[5]"));
if(!chkbox.isSelected())
{
chkbox.click();
System.out.println("True");
}
else {
System.out.println("false");
}
Upvotes: 0
Reputation: 1
boolean select = driver.findElementByXPath("xpath").isSelected();
Assert.assertEquals(select, "true");
If is selected the checkbox then this case will not get fail.
Upvotes: 0
Reputation: 11
What I have found that if your checkbox has tagname='input'
and type='checkbox'
, then only isSelected()
will return a boolean value.
Otherwise, we have to look for another attribute that is getting changed on the check and uncheck.
Upvotes: 1
Reputation: 2683
The problem might be with the xcode you are writing. Check if you can get a more unique xcode for the checkbox only something like
"/html/body/div[3]/div[2]/form/fieldset/div[1]/table/tbody/tr[10]/td/img[@type='checkbox']"
A checkbox xtype should mostly end with type=checkbox for isSelected to work
Upvotes: 0
Reputation: 1
In my view, isSelected() returns true only if it is a default selection. It may not work when we select an element and try to check if it is selected or not. Correct me if I am mistaken. Ankit
Upvotes: 0
Reputation: 26
If all else fails, try the is isDisplayed()
driver.findElement(By.xpath(xpath).isDisplayed()
Upvotes: 0
Reputation: 11
WebElement chck = driver.findElement(By.id("chkRemeberMe"));
if(!chck.isSelected())
{
System.out.println(" CheckBox Not Selected");
}
else
{
System.out.println("CheckBox Already Selected");
}
Upvotes: 1
Reputation: 3129
WebElement chBox = driver.findElement(By.id("chkIsActive"));
if (chBox.isSelected())
{
System.out.println("User active check box is already checked");
}
else
{
System.out.println("User active check box is not checked");
}
Hope this helps!
Upvotes: 4
Reputation: 2470
Check the class attribute is getting changed on Check/ Uncheck the check box. If so, then the selection state is stored as part of the class
String Class=chk.getAttribute("class");
if(Class.contains("class name when it is checked"))
{
System.out.println("Status: "+chk.getAttribute("checked"));
//This will return Null, since it is not a real check box(type=checkbox),
//so there is no checked attribute in it
}
else
{
System.out.println("Not Checked");
}
The isSelected()
method will not handle this type of checkbox, that's why it always returns false
or not checked(from your point of view)
Refer: here
Upvotes: 4
Reputation: 815
Your findElement() call is missing .click to check or select the check box or radio
WebElement we = findElement(By.xpath("some path));
we.click();
we.isSelected() => true
Upvotes: -1