2016-12-28 77 views
1

在可以包含更多行的表中,可以使用電子郵件ID搜索用戶。搜索後,選擇第一個複選框。但是硒拋出元素不可見的例外。無法檢查複選框,異常「元素不可見」

// * [@ id中= 'share_user']/tbody的/ TR 1/TD 1 //標籤

// * [@ id中= 'share_user']/tbody的/ TR 1/TD 1 //輸入

//標籤[@爲= 'checkbox_416']

最後標識符是動態的。仍然得到元素不可見的例外。下面

<table class="table table-striped table-hover dataTable" id="share_user" 
 
\t aria-describedby="share_user_info"> 
 
\t <thead> 
 
\t \t <tr role="row"> 
 
\t \t \t <th class="small-cell sorting" role="columnheader" tabindex="0" 
 
\t \t \t \t aria-controls="share_user" rowspan="1" colspan="1" style="width: 137px;" 
 
\t \t \t \t aria-label="Share Profile : activate to sort column ascending">Share Profile </th> 
 
\t \t \t <th class="medium-cell sorting" role="columnheader" tabindex="0" 
 
\t \t \t \t aria-controls="share_user" rowspan="1" colspan="1" style="width: 523px;" 
 
\t \t \t \t aria-label="User Details: activate to sort column ascending">User Details</th> 
 
\t \t \t <th class="medium-cell sorting_asc" role="columnheader" tabindex="0" 
 
\t \t \t \t aria-controls="share_user" rowspan="1" colspan="1" style="width: 366px;" 
 
\t \t \t \t aria-sort="ascending" aria-label="Access: activate to sort column descending">Access</th> 
 
\t \t </tr> 
 

 
\t </thead> 
 

 
\t <tbody role="alert" aria-live="polite" aria-relevant="all"> 
 
\t \t <tr class="odd"> 
 

 
\t \t \t <td class=""> 
 
\t \t \t \t <div class="checkbox check-default"> 
 

 
\t \t \t \t \t <input name="checkUser" id="checkbox_416" value="416" class="shareCheck" 
 
\t \t \t \t \t \t type="checkbox"> 
 
\t \t \t \t \t \t <label for="checkbox_416"></label> 
 
\t \t \t \t </div> 
 
\t \t \t </td> 
 
\t \t \t <td class=" "> 
 
\t \t \t \t <img src="" class="img-responsive display-inline share-image"> 
 
\t \t \t \t \t <div class="display-inline"> 
 

 
\t \t \t \t \t \t <p class="share-name">alexx</p> 
 
\t \t \t \t \t \t <p class="muted"> 
 
\t \t \t \t \t \t \t <i class="fa fa-envelope" hidden="true"></i> 
 
\t \t \t \t \t \t \t <span>[email protected]</span> 
 
\t \t \t \t \t \t </p> 
 
\t \t \t \t \t </div> 
 
\t \t \t </td> 
 

 
\t \t \t <td style="vertical-align: middle;" class=" sorting_1"> 
 

 
\t \t \t \t <div class="select2-container medium-cell" id="s2id_rolelink416" 
 
\t \t \t \t \t style="float:left;"> 
 
\t \t \t \t \t <a href="javascript:void(0)" onclick="return false;" class="select2-choice" 
 
\t \t \t \t \t \t tabindex="-1"> 
 
\t \t \t \t \t \t <span class="select2-chosen">PROFILE ADMIN</span> 
 
\t \t \t \t \t \t <abbr class="select2-search-choice-close"></abbr> 
 
\t \t \t \t \t \t <span class="select2-arrow"> 
 
\t \t \t \t \t \t \t <b></b> 
 
\t \t \t \t \t \t </span> 
 
\t \t \t \t \t </a> 
 
\t \t \t \t \t <input class="select2-focusser select2-offscreen" id="s2id_autogen11" 
 
\t \t \t \t \t \t type="text"> 
 
\t \t \t \t \t \t <div class="select2-drop select2-display-none select2-with-searchbox"> 
 
\t \t \t \t \t \t \t <div class="select2-search"> 
 
\t \t \t \t \t \t \t \t <input autocomplete="off" autocorrect="off" 
 
\t \t \t \t \t \t \t \t \t autocapitalize="off" spellcheck="false" class="select2-input" 
 
\t \t \t \t \t \t \t \t \t type="text"> 
 
\t \t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t \t <ul class="select2-results"> 
 
\t \t \t \t \t \t \t </ul> 
 
\t \t \t \t \t \t </div> 
 
\t \t \t \t </div> 
 
\t \t \t \t <select name="profileUsers.roleId" id="rolelink416" 
 
\t \t \t \t \t class="medium-cell select2-offscreen" style="float:left;" tabindex="-1"> 
 
\t \t \t \t \t <option value="6">PROFILE ADMIN</option> 
 
\t \t \t \t \t <option value="7">PROFILE AGENT</option> 
 
\t \t \t \t \t <option value="8">PROFILE VIEWER</option> 
 
\t \t \t \t \t <option value="196">profile role</option> 
 

 
\t \t \t \t </select> 
 

 
\t \t \t </td> 
 

 
\t \t </tr> 
 
\t </tbody> 
 
</table>

表代碼提到提供所述屏幕截圖以及。 xpath

html

回答

0

您可以使用下面的表達式,我已經驗證工作的罰款。

driver.findElement(By.xpath("//*[@id='share_user']/tbody/tr[1]/td[1]/div")).click(); 
1

嘗試使用明確的等待與Expected Conditions

WebDriverWait wait = new WebDriverWait(driver, 10); 
WebElement checkbox = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='share_user']/tbody/tr[1]/td[1]//input"))); 
checkbox.click(); 
+0

這是我跟蹤的過程。我得到的狀態是否勾選複選框,但我真的不明白爲什麼這是投擲元素不可見,現在至少跑了30次。 –

+0

@NagarjunaReddy如果該複選框是通過JavaScript添加的,則可能需要一些時間直到它被認爲對Selenium可見。 – Guy

0

試試這個

int ele_size=driver.findElements(By.xpath("locator")).size(); 

driver.findElements(By.xpath("locator")).get(ele_size-1).click(); 

我們可能會遇到的元素不可見異常,如果元件匹配定位器的DOM存在但目前不可見。所以我們必須在第一個語句中取第一個元素的大小,然後在下一個語句中我們必須從列表中取第一個元素並單擊元素。

0

使用此//*[@id='share_user']/tbody/tr[1]/td[1]/div工作