简体   繁体   中英

How to check the condition, whether the checkbox is clicked or not using java with selenium

When i tried to check the condition whether the checkbox is clicked or not. but it displayed only the else(false) part, when the condition is true or false.

Checkbox image: 在此处输入图片说明

HTML :

<tr class="ui-widget-content ui-datatable-even ui-datatable-selectable ui-state-highlight" data-ri="0" data-rk="69022768" role="row" aria-selected="true"> 
    <td class="ui-selection-column" role="gridcell" style="width: 34px; text-align:center"> 
        <div class="ui-chkbox ui-widget"> 
            <div class="ui-helper-hidden-accessible"> 
                <input name="identititesSelect:tabView:searchResultsTable_checkbox" type="checkbox"> 
            </div> 

Here is the below code which i tried:

 WebDriverWait wait = new WebDriverWait(d1, 30);
              WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='ui-helper-hidden-accessible']/input[@type='checkbox']")));

              if (element.isSelected())
              {
              System.out.println("true");
              }
              else
              {
              System.out.println("false");
              }

You use wrong selector. XPath should be as follow:

WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath('//div[@class="ui-helper-hidden-accessible"]/input[@type="checkbox"]')));

You also can check attribute aria-selected which is true if checkbox selected, and false otherwise:

WebElement element = d1.findElement(By.xpath("tr[@class='ui-widget-content ui-datatable-even ui-datatable-selectable']"));
if (element.getAttribute('aria-selected').equals('true'))
    {
    System.out.println("true");
    }
    else
    {
    System.out.println("false");
    }

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