簡體   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