2017-11-11 121 views
1

我有一個表,它的HTML代碼如下所示。我想單擊包含「基本用戶」的行中的複選框(即在tr [5]中)。我無法點擊複選框。我使用firefox瀏覽器和硒的webdriver(JAVA)硒的webdriver的java:點擊複選框

<div class="ui-jqgrid-bdiv" style="height: 340px; width: 440px;"> 
<div style="position:relative;"> 
<div></div> 
<table id="s_4_l" class="ui-jqgrid-btable" border="0" cellspacing="0" cellpadding="0" tabindex="0" role="grid" aria-multiselectable="true" aria-labelledby="" style="width: 439px;" summary="Responsibilities" datatable="1"> 
<tbody> 
<tr class="jqgfirstrow" style="height:auto" role="row"> 
<tr id="1" class="ui-widget-content jqgrow ui-row-ltr" tabindex="-1" role="row" style="height: 32px;"> 
<tr id="2" class="ui-widget-content jqgrow ui-row-ltr" tabindex="-1" role="row" style="height: 32px;"> 
<tr id="3" class="ui-widget-content jqgrow ui-row-ltr" tabindex="-1" role="row" style="height: 32px;"> 
<tr id="4" class="ui-widget-content jqgrow ui-row-ltr" tabindex="-1" role="row" style="height: 32px;" aria-selected="false"> 
<tr id="5" class="ui-widget-content jqgrow ui-row-ltr ui-state-highlight selected-row ui-state-hover" tabindex="-1" role="row" style="height: 32px;" aria-selected="true"> 
<td aria-describedby="s_4_l_cb" style="text-align:center;display:none;" role="gridcell"> 
<input id="jqg_s_4_l_5" class="cbox" type="checkbox" role="checkbox"> 
</td> 
<td id="5_s_4_l_SSA_Primary_Field" class="edit-cell ui-state-highlight" title="Unchecked" style="text-align:center;" role="gridcell" aria-labelledby="s_4_l_SSA_Primary_Field s_4_l_altCheckBox" tabindex="0"> 
<span tabindex="-1"> 
<input id="5_SSA_Primary_Field" class="customelement siebui-ctrl-checkbox siebui-align-center siebui-input-align-center s_4_2_82_0" type="checkbox" aria-checked="false" value="N" tabindex="0" role="checkbox" name="SSA_Primary_Field" maxlength="1" aria-labelledby="5_s_4_l_Name s_4_l_SSA_Primary_Field s_4_l_altCheckBox"> 
</span> 
</td> 
<td id="5_s_4_l_Name" title="Base User" style="text-align:left;" role="gridcell" aria-labelledby="s_4_l_Name">Base User</td> 
<td id="5_s_4_l_Organization" title="Canada" style="text-align:left;" role="gridcell" aria-labelledby="s_4_l_Organization">Canada</td> 
</tr> 
<tr id="6" class="ui-widget-content jqgrow ui-row-ltr" tabindex="-1" role="row" style="height: 32px;"> 
<td aria-describedby="s_4_l_cb" style="text-align:center;display:none;" role="gridcell"> 
<td id="6_s_4_l_SSA_Primary_Field" title="Checked" style="text-align:center;" role="gridcell" aria-labelledby="s_4_l_SSA_Primary_Field s_4_l_altCheckBox"> 
<span class="siebui-icon-checkbox-checked"> 
<img hspace="0" border="0" space="0" title="Selected" alt="Selected" src="XYZ.com/images/check_d.gif"> 
</span> 
<span style="display:none;">Y</span> 
</td> 
<td id="6_s_4_l_Name" title="Product Administrator" style="text-align:left;" role="gridcell" aria-labelledby="s_4_l_Name">Product Administrator</td> 
<td id="6_s_4_l_Organization" title="Canada" style="text-align:left;" role="gridcell" aria-labelledby="s_4_l_Organization">Canada</td> 
</tr> 
<tr id="7" class="ui-widget-content jqgrow ui-row-ltr" tabindex="-1" role="row" style="height: 32px;"> 
</tbody> 
</table> 

此表包含7行,第一列各行中的複選框。在任何給定時間,只有一行有複選框CHECKED/TICKED,這裏是第6行。對於其他行,複選框不顯示。只有當您在某一行的第一個單元格區域內單擊某處時,該行的複選框纔會顯示。然後你可以勾選該複選框。因此,我點擊了第五行的第一個單元格,現在顯示了複選框(如下面的代碼所示)。

如何點擊此複選框第5行的? 我的代碼如下,第三行是一個我試圖點擊複選框,但它不會點擊。

WebElement primaryCell = driver.findElement(By.xpath("/html/body/div[9]/div[2]/div/div/div/div[3]/form/div[1]/div/div/div[3]/div[3]/div/table/tbody/tr/td[contains(text(),'Base User')]/preceding-sibling::td[@title='Unchecked']")); 
primaryCell.click(); 
primaryCell.findElement(By.xpath("//span/input")).click(); 

請幫忙,謝謝!

請注意,標記名錶,TR的@id,TD - 不是靜態的,不斷變化。 我曾嘗試使用某些現有帖子中提供的解決方案,但我無法解決我的問題。

回答

0

嘗試下面的代碼,它使用簡單的XPath表達式。
如果單擊單元格,然後你必須等待,直到出現在頁面上的複選框,並clikable,它不會發生馬上,第一次點擊觸發一些JavaScript,這使得該複選框可見光和點擊,它必須需要一定的時間,通常幾十毫秒,但是如果任何網絡連接或服務器或計算機或所有的人都慢,則有時可能需要幾秒鐘,甚至在幾秒鐘:

String cell = "//table[@summary='Responsibilities']//tr[ contains(., 'Base User')]/td[2]"; 

String checkbox = cell + "//input"; 

driver.findElement(By.xpath(cell)).click(); 

WebDriverWait wait = new WebDriverWait(driver, 10); 

wait.until(ExpectedConditions.elementToBeClickabe(By.xpath(checkbox))).click(); 
+0

真棒十幾個,非常感謝你很多krokodilko,我很欣賞。 – Sanjay