硒的webdriver的java:点击复选框

硒的webdriver的java:点击复选框

问题描述:

我有一个表,它的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 - 不是静态的,不断变化。 我曾尝试使用某些现有帖子中提供的解决方案,但我无法解决我的问题。

尝试下面的代码,它使用简单的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