2

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>&nbsp;</td><td>              
                    &nbsp;</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>&nbsp;</td><td>&nbsp;</td>
            </tr>
        </table>
    </div>    

</div>
    </body>
</html>

//结尾

编辑: 使用 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那么它应该选择相关的复选框,例如:

如果我通过 Checkbox Text =Some text present.....那么 Checkbox Idctl00_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...............

在此处输入图像描述

4

3 回答 3

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;

        }
}

希望这可以帮助。

于 2012-09-19T15:51:49.243 回答
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"))

希望这对您有所帮助)

于 2012-09-19T15:11:04.560 回答
0

您可以使用 XPath 表达式,例如:

driver.find_element_by_xpath("//input[normalize-space(../label) = 'Item 1']")
于 2012-09-19T15:33:39.470 回答