繁体   English   中英

如何使用带有硒的java检查条件,是否单击复选框

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

当我尝试检查复选框是否被点击时。 但当条件为真或假时,它只显示 else(false) 部分。

复选框图片: 在此处输入图片说明

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> 

这是我尝试过的以下代码:

 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");
              }

您使用了错误的选择器。 XPath应如下所示:

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

您还可以检查属性aria-selected如果选中复选框则为true ,否则为false

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");
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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