首页 文章

Selenium Webdriver捕获复选框文本

提问于
浏览
1

您好我正在尝试捕获同一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 回答

  • 2

    使用以下代码找到Label:

    //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")
    

相关问题