为这个庞大的列表制作一个更紧凑的代码

时间:2015-12-17 10:38:12

标签: java loops selenium checkbox

所以我想让下面的代码更紧凑,有办法吗?

可能有一个循环或其他东西。尝试了一个for循环,但没有真正解决这个问题......

        driver.findElement(By.xpath("//input [@value='1']")).click();
        driver.findElement(By.xpath("//input [@value='2']")).click();
        driver.findElement(By.xpath("//input [@value='3']")).click();
        driver.findElement(By.xpath("//input [@value='4']")).click();
        driver.findElement(By.xpath("//input [@value='5']")).click();
        driver.findElement(By.xpath("//input [@value='6']")).click();
        driver.findElement(By.xpath("//input [@value='7']")).click();
        driver.findElement(By.xpath("//input [@value='8']")).click();
        driver.findElement(By.xpath("//input [@value='9']")).click();
        driver.findElement(By.xpath("//input [@value='10']")).click();
        driver.findElement(By.xpath("//input [@value='11']")).click();
        driver.findElement(By.xpath("//input [@value='12']")).click();
        driver.findElement(By.xpath("//input [@value='13']")).click();
        driver.findElement(By.xpath("//input [@value='14']")).click();
        driver.findElement(By.xpath("//input [@value='15']")).click();
        driver.findElement(By.xpath("//input [@value='16']")).click();
        driver.findElement(By.xpath("//input [@value='17']")).click();
        driver.findElement(By.xpath("//input [@value='18']")).click();
        driver.findElement(By.xpath("//input [@value='19']")).click();
        Thread.sleep(1000);
        driver.findElement(By.xpath("/html/body/div[1]/section/div[1]/div[2]/form/div/div[2]/div[1]/div[2]/div[7]/div/input[1]")).click();
        driver.findElement(By.xpath("/html/body/div[1]/section/div[1]/div[2]/form/div/div[2]/div[1]/div[2]/div[7]/div/input[2]")).click();
        driver.findElement(By.xpath("/html/body/div[1]/section/div[1]/div[2]/form/div/div[2]/div[1]/div[2]/div[7]/div/input[3]")).click();
        driver.findElement(By.xpath("/html/body/div[1]/section/div[1]/div[2]/form/div/div[2]/div[1]/div[2]/div[7]/div/input[4]")).click();
        driver.findElement(By.xpath("/html/body/div[1]/section/div[1]/div[2]/form/div/div[2]/div[1]/div[2]/div[7]/div/input[5]")).click();
        driver.findElement(By.xpath("/html/body/div[1]/section/div[1]/div[2]/form/div/div[2]/div[1]/div[2]/div[7]/div/input[6]")).click();
        driver.findElement(By.xpath("/html/body/div[1]/section/div[1]/div[2]/form/div/div[2]/div[1]/div[2]/div[7]/div/input[7]")).click();
        driver.findElement(By.xpath("/html/body/div[1]/section/div[1]/div[2]/form/div/div[2]/div[1]/div[2]/div[7]/div/input[8]")).click();
        driver.findElement(By.xpath("/html/body/div[1]/section/div[1]/div[2]/form/div/div[2]/div[1]/div[2]/div[7]/div/input[9]")).click();
        driver.findElement(By.xpath("/html/body/div[1]/section/div[1]/div[2]/form/div/div[2]/div[1]/div[2]/div[7]/div/input[10]")).click();
        driver.findElement(By.xpath("/html/body/div[1]/section/div[1]/div[2]/form/div/div[2]/div[1]/div[2]/div[7]/div/input[11]")).click();
        driver.findElement(By.xpath("/html/body/div[1]/section/div[1]/div[2]/form/div/div[2]/div[1]/div[2]/div[7]/div/input[12]")).click();
        driver.findElement(By.xpath("/html/body/div[1]/section/div[1]/div[2]/form/div/div[2]/div[1]/div[2]/div[7]/div/input[13]")).click();
        driver.findElement(By.xpath("/html/body/div[1]/section/div[1]/div[2]/form/div/div[2]/div[1]/div[2]/div[7]/div/input[14]")).click();
        driver.findElement(By.xpath("/html/body/div[1]/section/div[1]/div[2]/form/div/div[2]/div[1]/div[2]/div[7]/div/input[15]")).click();
        driver.findElement(By.xpath("/html/body/div[1]/section/div[1]/div[2]/form/div/div[2]/div[1]/div[2]/div[7]/div/input[16]")).click();
        driver.findElement(By.xpath("/html/body/div[1]/section/div[1]/div[2]/form/div/div[2]/div[1]/div[2]/div[7]/div/input[17]")).click();
        driver.findElement(By.xpath("/html/body/div[1]/section/div[1]/div[2]/form/div/div[2]/div[1]/div[2]/div[7]/div/input[18]")).click();
        driver.findElement(By.xpath("/html/body/div[1]/section/div[1]/div[2]/form/div/div[2]/div[1]/div[2]/div[7]/div/input[19]")).click();
        driver.findElement(By.xpath("/html/body/div[1]/section/div[1]/div[2]/form/div/div[2]/div[1]/div[2]/div[7]/div/input[20]")).click();

        driver.findElement(By.xpath("//button [@type='submit']")).click();
        Thread.sleep(10000);

这是带有复选框的两个列表。

1 个答案:

答案 0 :(得分:5)

怎么样

    for(int i = 1; i < 20; ++i) {
      driver.findElement(By.xpath("//input [@value='"+i+"']")).click();
    }
    Thread.sleep(1000);

以及其他项目等等?