简体   繁体   中英

Selenium webdriver webelemenet.isSelected() returns incorrect values

I am trying to check if the checkbox is checked or not, for this I am using isSelected. My checkbox is a div

div class="mainSprite chkBox" data-dojo-attach-event="onclick:toggle" data-dojo-attach-point="chkNode"

I find the div with xpath and isSelected always returns false irrespective of weather the checkbox is checked or not. I am using slenium webdriver 2.40 and FF28

I had a similar situation. One clue is:

<table id="dijit__TemplatedMixin_14" class="chkBoxCntr chkBoxChecked"...

Using FireBug I had to check and uncheck the textbox and look at the html for what attribute for my element was different. Then I wrote a jQuery to find the difference. Below is a sample function with 2 different return statements that i've used.

@SuppressWarnings("unchecked")
    public List<WebElement> getSelectedItems() throws InterruptedException {


        List<WebElement> list =  (List<WebElement>) ((JavascriptExecutor) driver)
                .executeScript("return jQuery.find('[class*=\"FIChecked\"]')");
        //.executeScript("return jQuery.find('[checked=\"checked\"]')");

        return list;
    }

Use it as a starting point and play with the search a bit. My guess is that

<table id="dijit__TemplatedMixin_14" class="chkBoxCntr chkBoxChecked" widgetid="dijit__TemplatedMixin_14">

will switch to

 <table id="dijit__TemplatedMixin_14" class="chkBoxCntr" widgetid="dijit__TemplatedMixin_14">

when the item is not selected. So calling getSelectedItems().size should equal 0 if every box was unchecked.

If in your case, the XPATH of the checkbox is looks like //*[@id='XXXXX']/div[2] .

Then the in HTML, go to the path, //*[@id='XXXXX']/div[1]/input . This will have type="checkbox" .

Use the "isSelected" method on that

But if you want to select the checkbox, you should use //*[@id='XXXXX']/div[2] only.

If it is not a checkbox and a div is seems like a checkbox you need to create a customised function which will reture you boolean value on the basis of the changed attribute of div when you click on it.

In one of my case when I am clicking in this kind of div cum checkbox an additional class is adding in the class attribute of div tag.

function will look like,

public boolean checkStatus(WebElement element,String attribute,String value){

if(element.getAttribute(attribute).contains(value){
return true;
}
else{
return false;
}

}

and call the method like

obj.checkStatus(ele,"class","chkTrue");

where "chkTrue" is the additional class added in div.

When the div checkbox is checked, it's @class, or it's ancestor's @class should change to
".. .. .*checked" class, judge on this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM