2013-05-10 64 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")); 
} 
相关问题