硒网络驱动程序捕获复选框文本



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

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

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

当使用此获取文本时,但我得到空文本。 使用 getAttribute 时,我得到的复选框的实际名称是 $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>

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

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

谢谢。

使用以下代码查找标签:

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

最新更新