Checkbox-Attribute value | Selenium Forum
M
Posted on 16/01/2017
Iam working on keyword frame work and struck in using "checkbox" option.

In below code, i tried to capture checkbox in String with ".getAttribute(type)". But always "type" is having value and unable to navigate further.

Can you please provide your input in fixing this?

[*][*][*][*][*][*][*][*][*][*][*][*][*][*]
public String checkCheckBox(String data,String b){

App_logs.debug("check check box");

try {

String checkbox = d.findElement(By.xpath(OR.getProperty(b))).getAttribute("type");

if (checkbox == null) {
d.findElement(By.xpath(OR.getProperty(b))).click();
}
} catch (Exception e) {
return Constants.Fail + "---" + e.getMessage();
}
return Constants.pass;

}

[*][*][*][*][*][*][*][*][*][*][*][*][*][*][*]

[attachment=0:2rrcyth4]checkbox-attribute.png[/attachment:2rrcyth4]

M
Replied on 17/01/2017

i'm unable to understand what do you want?


M
Replied on 17/01/2017

can you please help me in checking/unchecking the check box from below screenshot.

This is xpath which i used...........

d.findElement(By.xpath(OR.getProperty(b))).getAttribute("type");


M
Replied on 18/01/2017

use this to click, what is the problem?

d.findElement(By.xpath(OR.getProperty(b))).click();


M
Replied on 18/01/2017

Keyword function: "checkCheckBox"

Before clicking on check box we are supposed to validate whether it is NULL or any value exists. To validate this, we tried to save that value in as String as below.

try {

String checkbox = d.findElement(By.xpath(OR.getProperty(b))).getAttribute("type");

if (checkbox == null) {
d.findElement(By.xpath(OR.getProperty(b))).click();
}
} catch (Exception e) {
return Constants.Fail + "---" + e.getMessage();
}
return Constants.pass;

}


M
Replied on 18/01/2017

can i have the access to the website?


M
Replied on 20/01/2017

This URL cannot be accessed. I can share my screen through webex.


M
Replied on 21/01/2017

send a full screenshot of HTML of the checkbox. in above checkbox, the screenshot is cut from the right.


M
Replied on 23/01/2017

This is screenshot.

Responsive image

M
Replied on 25/01/2017

try this and tell me the result

try {

String checkbox = d.findElement(By.xpath(OR.getProperty(b))).getAttribute("selected");

if (checkbox == null) {
d.findElement(By.xpath(OR.getProperty(b))).click();
}
} catch (Exception e) {
return Constants.Fail + "---" + e.getMessage();
}
return Constants.pass;

}


M
Replied on 27/01/2017

This is working fine. can you please let me know from where did you use attribute "selected" as this is not populated in HTML of checkbox


M
Replied on 27/01/2017

I remember "selected" from when I learned php and html. it works with radio button too.