如何使用selenium webdriver获取多个chekcbox标签名称?

时间:2017-10-18 03:25:58

标签: java selenium xpath selenium-webdriver

如何使用selenium webdriver获取多个复选框标签名称?

以下是网站url

2 个答案:

答案 0 :(得分:0)

假设我想要获取名为Multiple Checkbox Group Test 1

的所有复选框标签名称组
driver.get("http://www.javascriptsource.com/forms/check-uncheck-multiple-checkboxes.html");
List<WebElement> all = driver.findElements(By.xpath("//legend[text()='Multiple Checkbox Group Test 1']/..//following-sibling::label"));    //get the all labels from particular group
System.out.println(all.size());

for(int i=0; i<all.size(); i++)
{
    System.out.println(all.get(i).getText());
    Thread.sleep(2500); 
}

答案 1 :(得分:0)

With reference to shared link,<br/>

code: <br/>
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class test {

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); //input[@type='checkbox']/following-sibling::label
        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://www.javascriptsource.com/forms/check-uncheck-multiple-checkboxes.html");
        List<WebElement> checkboxLabels = driver.findElements(By.xpath("//input[@type='checkbox']/following-sibling::label"));
        for(WebElement checkboxLabel: checkboxLabels) {
            System.out.println(checkboxLabel.getText());
        }
    }
}

output: <br/>
chkboxarray 1<br/>
chkboxarray 2<br/>
chkboxarray 3<br/>
extrachkbox 1<br/>
groupa 1<br/>
groupa 2<br/>
groupa 3<br/>
groupb 1<br/>
groupb 2<br/>
groupb 3