HTML源页面:
//开始:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
<div id="ctl00_ContentPlaceHolder1_control1_pnlEdit">
<h2>
<span id="ctl00_ContentPlaceHolder1_control1_lblEditHeader">Add New</span></h2>
<br /><br />
<div>
<table class="DetailsView" cellspacing="0" cellpadding="5" rules="all" border="1" id="ctl00_ContentPlaceHolder1_control1" style="height:50px;width:600px;border-collapse:collapse;">
<tr>
<td> </td><td>
</td>
</tr><tr>
<td>Category</td><td>
<div style="border-style: solid; border-width: 1px; border-color: inherit; overflow:auto; height:200px; width:300px;" >
<table id="ctl00_ContentPlaceHolder1_control1_lstCat" class="CheckBoxList" selectionmode="Multiple" border="0">
<tr>
<td><span style="padding-left:0px;"><input id="ctl00_ContentPlaceHolder1_control1_lstCat_0" type="checkbox" name="ctl00$ContentPlaceHolder1$control1$dv$lstCat$0" /><label for="ctl00_ContentPlaceHolder1_control1_lstCat_0">Item
1</label></span></td>
</tr><tr>
<td><span style="padding-left:15px;"><input id="ctl00_ContentPlaceHolder1_control1_lstCat_1" type="checkbox" name="ctl00$ContentPlaceHolder1$control1$dv$lstCat$1" />Item
1-2</span></td>
</tr><tr>
<td><span style="padding-left:15px;"><input id="ctl00_ContentPlaceHolder1_control1_lstCat_2" type="checkbox" name="ctl00$ContentPlaceHolder1$control1$dv$lstCat$2" /><span>Item
1-3</span></span></td>
</tr><tr>
<td><span style="padding-left:15px;"><input id="ctl00_ContentPlaceHolder1_control1_lstCat_3" type="checkbox" name="ctl00$ContentPlaceHolder1$control1$dv$lstCat$3" /><span>Item
1-4</span></span></td>
</tr><tr>
<td><span style="padding-left:0px;"><input id="ctl00_ContentPlaceHolder1_control1_lstCat_4" type="checkbox" name="ctl00$ContentPlaceHolder1$control1$dv$lstCat$4" /><span>Item
2</span></span></td>
</tr><tr>
<td><span style="padding-left:15px;"><input id="ctl00_ContentPlaceHolder1_control1_lstCat_5" type="checkbox" name="ctl00$ContentPlaceHolder1$control1$dv$lstCat$5" /><span>Item
2-1</span></span></td>
</tr><tr>
<td><span style="padding-left:0px;"><input id="ctl00_ContentPlaceHolder1_control1_lstCat_6" type="checkbox" name="ctl00$ContentPlaceHolder1$control1$dv$lstCat$6" /><span>Item
3</span></span></td>
</tr><tr>
<td><span style="padding-left:15px;"><input id="ctl00_ContentPlaceHolder1_control1_lstCat_7" type="checkbox" name="ctl00$ContentPlaceHolder1$control1$dv$lstCat$7" /><span>Item
3-1</span></span></td>
</tr>
</table>
</div>
</td>
</tr><tr>
<td> </td><td> </td>
</tr>
</table>
</div>
</div>
</body>
</html>
//结束
编辑: Selecting Element followed by text with Selenium WebDriver
我的问题类似于上面的帖子
结束编辑
以下是我在运行时呈现的页面源代码
我有一个唯一的复选框ID,为每个复选框生成一个示例:
ctl00_ContentPlaceHolder1_Control1_stCat_1
ctl00_ContentPlaceHolder1_Control1_stCat_2
ctl00_ContentPlaceHolder1_Control1_stCat_3
..........................................
ctl00_ContentPlaceHolder1_Control1_stCat_8
etc..............
我的要求是:如果我通过Checkbox Text
,那么它应该选中相关的复选框
举个例子:
如果我通过复选框文字= Some text present.....
,则应检查复选框ID ctl00_ContentPlaceHolder1_Control1_stCat_1
PS:Item 1, Item 2
只是真实数据中的一个例子我有一些随机名称
以下是我的html代码生成:
<tr>
<td>Category</td>
<td>
<div style="border-style: solid; border-width: 1px; border-color: inherit; overflow:auto; height:200px; width:300px;">
<table id="ctl00_ContentPlaceHolder1_AddControl1_lstCat" class="CheckBoxList" border="0" selectionmode="Multiple">
<tbody>
<tr>
<td>
<span style="padding-left:15px;">
<input id="ctl00_ContentPlaceHolder1_Control1_stCat_1" type="checkbox" name="ctl00$ContentPlaceHolder1$AddControl1$cat$lstCat$1"/>
<label for="ctl00_ContentPlaceHolder1_AddControl1_lstCat_8">Item 1</label>
</span>
</td>
</tr>
<tr>
<td>
<span style="padding-left:15px;">
<input id="ctl00_ContentPlaceHolder1_Control1_stCat_2" type="checkbox" name="ctl00$ContentPlaceHolder1$AddControl1$cat$lstCat$2"/>
<label for="ctl00_ContentPlaceHolder1_AddControl1_lstCat_2">Item 2</label>
</span>
</td>
</tr>
<tr>
<td>
<span style="padding-left:15px;">
<input id="ctl00_ContentPlaceHolder1_Control1_stCat_3" type="checkbox" name="ctl00$ContentPlaceHolder1$AddControl1$cat$lstCat$3"/>
<label for="ctl00_ContentPlaceHolder1_AddControl1_lstCat_3">Item 3</label>
</span>
</td>
</tr>
.............
..............
................
.................
more...............
答案 0 :(得分:2)
int i=1;
while(!driver.findElement(By.id("ctl00_ContentPlaceHolder1_Control1_stCat_"+i)).equals(null)){
if(driver.findElement(By.id("ctl00_ContentPlaceHolder1_Control1_stCat_"+i)).getText()="Item1"){
driver.findElement(By.id("ctl00_ContentPlaceHolder1_Control1_stCat_"+i)).click();
break;
}
}
希望这有帮助。
答案 1 :(得分:0)
实际上你可以使用另一种方法。
String cssSelectorChckbox1= "table[id='ctl00_ContentPlaceHolder1_AddControl1_lstCat']" tbody tr td>input[id='ctl00_ContentPlaceHolder1_Control1_stCat_1']";
String cssSelectorChckboxLabel1="table[id='ctl00_ContentPlaceHolder1_AddControl1_lstCat']" tbody tr td>label[for='ctl00_ContentPlaceHolder1_AddControl1_lstCat_8']";
//then you can use 2 js methods: one for click checkbox, second one for validation label text near chckbox:
public void clickOnTheElement(String cssLocator){
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\'"+cssLocator+"\');");
stringBuilder.append("x.click();");
js.executeScript(stringBuilder.toString());
}
public String getTextOfTheElement(String cssLocator)
{ JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\""+cssLocator+"\");");
stringBuilder.append("return x.text().toString();") ;
String res= (String) js.executeScript(stringBuilder.toString());
return res;
}
//using first method you are able to click on checkbox.
clickOnTheElement( cssSelectorChckbox1);
//using second method you are able to verify that label near checkbox is correct:
String actualCheckboxLabel=getTextOfTheElement(cssSelectorChckboxLabel1);
Assert.assertTrue(actualCheckboxLabel.equals("expected text near checkbox"))
希望这能以某种方式帮助你)
答案 2 :(得分:0)
您可以使用XPath表达式,如:
driver.find_element_by_xpath("//input[normalize-space(../label) = 'Item 1']")