2013-05-10 196 views
0

我一直在嘗試獲取頁面中所有複選框內的標籤值,到目前爲止,我一直在使用此代碼來自動執行下面的HTML。如何獲得所有複選框的文本(標籤)

JAVA

List<WebElement> allCBox = driver.findElements(By.xpath("//input[@type='checkbox']")); 
for(WebElement checkbox : allCBox) { 
    System.out.println("--- "+checkbox.gettext()); 
}  

HTML:

<html> 
    <p> Regions: 
    south <input type="checkbox" name="south" value="south">&nbsp; 
    british <input type="checkbox" name="british" value="british">&nbsp; 
    north <input type="checkbox" name="north" value="north">&nbsp; 
    southeast <input type="checkbox" name="southeast" value="southeast">&nbsp; 
    west <input type="checkbox" name="west" value="west">&nbsp; 
    Spanish <input type="checkbox" name="spanish" value="spanish">&nbsp; 
    europian <input type="checkbox" name="europian" value="europian">&nbsp; 
    northeast <input type="checkbox" name="northeast" value="northeast"> 
    </p> 
</html> 
+0

您需要標記您的嵌入式html;它沒有正確顯示。 – 2013-05-10 10:19:41

回答

0
嘗試

以下邏輯。

List<WebElement> checkboxList=driver.findElements(By.cssSelector("[type='checkbox']")) 
for(WebElement checkbox : CHECKBOXlist) 
{ 
    if(checkbox.getAttribute("name").length()>=6) 
    { 
      checkbox.click(); 
    } 
} 

FYI:我已經看到here

3

你的HTML頁面在你的情況<input>沒有文本屬性,但標籤等於「名」和「值」屬性。所以你可以獲得「名稱」或「值」屬性。

List<WebElement> CHECKBOXlist = driver.findElements(By.xpath("//input[@type='checkbox']")); 

for(WebElement checkbox : CHECKBOXlist) { 
    System.out.println(checkbox.getAttribute("name")); 
} 
相關問題