2017-07-14 11 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; 

次のあなたは、次のようにチェックボックスのテキストをretriveするために、以下を使用することができます。

String my_label = constructionMgmnt_label.getAttribute("innerHTML") 

これがあなたの質問に答えるかどうか教えてください。

+0

クイックリプレイをありがとう。 –

+0

@TomaszWidaあなたの質問に応えたら答えを受け入れてください。ありがとう – DebanjanB