2017-07-14 73 views
0

你好我试图捕获名称位于标签标签中的复选框旁边的名称是在相同的跨度类中。当试图使用getText()方法捕获文本时,我得到空名称。Selenium Webdriver捕获复选框文本

在代码中显示它如何操作。

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

使用此getText但我得到空的文本。使用的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']")); 

谢谢。

回答

1

这里是回答你的问题:

使用下面的代码来定位标签:

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

让我知道这个答案是否是您的问题。

+0

感谢您的快速回放。 –

+0

@TomaszWida如果迎合您的问题,请接受答案。谢谢 – DebanjanB

相关问题