Selenium Webdriver捕获复选框文本

时间:2017-07-14 13:30:42

标签: selenium selenium-webdriver checkbox

您好我正在尝试捕获同一span类中的复选框旁边的名称,但名称位于label标签中。当尝试使用getText()方法捕获文本时,我得到空名。

在代码中显示它我是如何做的。

//to find the checkbox
@FindBy(how = How.ID, id = "ConstructionManagement")
private WebElement constructionMgmnt;

使用此getText但我得到空文本。当使用getAttribute我得到Checkbox的实际名称是$ PpyWorkPage $ pOutageRequest $ pConstructionManagement

constructionMgmnt.getText();
constructionMgmnt.getAttribute("name")

页面来源如何编码复选框。

<span class="checkbox" data-ctl="Checkbox">
   <input value="false" name="$PpyWorkPage$pOutageRequest$pConstructionManagement" type="hidden">
   <input id="ConstructionManagement" class="checkbox chkBxCtl" value="true" name="$PpyWorkPage$pOutageRequest$pConstructionManagement" validationtype="true-false" pn=".ConstructionManagement" type="checkbox">
   <label class=" cb_standard" for="ConstructionManagement">Construction Management</label>
</span>

有没有人能想到如何从指向Checkbox ID的元素中获取文本,或者我必须使用xpath标记来获取该复选框旁边的文本?

//example of garbing the name of the Label (I tested this and it works)
driver.findElement(By.xpath("//label[@for='ConstructionManagement']"));

谢谢。

1 个答案:

答案 0 :(得分:2)

使用以下代码找到标签:

//to find the Label Text
@FindBy(how = How.ID, xpath = "//input[@id='ConstructionManagement')//following::label[1]"
private WebElement constructionMgmnt_label;

接下来,您可以使用以下内容来检索复选框文本,如下所示:

String my_label = constructionMgmnt_label.getAttribute("innerHTML")