如何使用Selenium Java WebDriver检查复选框?

时间:2016-06-27 14:15:39

标签: java html selenium checkbox selenium-webdriver

我试图想办法检查表格中的复选框。通常将它们定义为type =' checkbox'。因此,我发现很难使用webDriver来检查复选框,因为它们位于标签中。

下面给出了一个示例HTML代码。



<tbody id="gridview-2345-body">
	<tr id="gridview-2345-record-/DNA/Study1_HS.xml" class="x4-grid-row x4-grid-data-row x4-grid-row-selected" data-boundview="gridview-1270" role="row">
		<td id="ext4-ext-gen1234" class="x4-grid-cell x4-grid-td" role="gridcell">
			<div class="x4-grid-cell-inner " style="text-align:left;" unselectable="on">
				<div class="x4-grid-row-checker"/>
			</div>
		</td>
		<td id="ext4-ext-1235" class="x4-grid-cell x4-grid-td" role="gridcell">
			<div class="x4-grid-cell-inner " style="text-align:left;" unselectable="on">
				<span id="ext4-icon1568" class="fa fa-file-code-o labkey-file-icon"/>
			</div>
		</td>
		<td id="ext4-ext-gen1236" class="x4-grid-cell x4-grid-td" role="gridcell">
			<div class="x4-grid-cell-inner " style="text-align:left;" unselectable="on">
				<div width="100%" height="16px">
					<div style="float: left;"/>
					<div style="padding-left: 8px; white-space:normal !important;">
						<span style="display: inline-block; white-space: nowrap;">Study1_HS.xml</span>
					</div>
				</div>
			</div>
		</td>
	</tr>
</tbody>
&#13;
&#13;
&#13;

我尝试使用&#39;包含&#39;在xpath中

driver.findElement(By.xpath("//*[contains(@id, 'Study1_HS.xml')]/td[1]/div/div")).click();

2 个答案:

答案 0 :(得分:0)

我想知道......因为当选中复选框时TR包含类更改,可能单击TR将触发检查。试试看,看看。

String searchText = "Study1_HS.xml";
List<WebElement> rows = driver.findElements(By.tagName("tr"));
for (WebElement row : rows)
{
    if (row.getText().contains(searchText))
    {
        row.click();
        break;
    }
}

答案 1 :(得分:0)

所以我用过&#39;前面的&#39;在xpath中使它工作

//span[text()='Study1_HS.xml']/preceding::td/div/div[@class='x4-grid-row-checker']

http://www.xpathtester.com/xpath/b1d50008dd4be8ab7545548c4b8238f5