如何在Java中使用WebDriver选中多个复选框并进行验证? [英] How to select multiple check box with webdriver in java and verify?

查看:161
本文介绍了如何在Java中使用WebDriver选中多个复选框并进行验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此页面上工作 http://www.samsung.com/us/视频/电视/所有产品 并在下面的代码段中写下以选中多个复选框. 但这只是选择第一个复选框,然后给我以下消息:在缓存中找不到元素-自查找以来,页面可能已更改" 谁能建议,我需要为此做什么?

Working on this page http://www.samsung.com/us/video/tvs/all-products and wrote below piece of code to select multiple checkboxes. But it is just selecting first checkbox and then giving me message that " element not found in the cache - perhaps the page has changed since it was looked up" Can anyone suggest, What I need to do for this?

enter code here

List <WebElement> checkBoxes =driver.findElements(By.xpath("//input[@name='filterOption']"));
   for(int i=0; i<checkBoxes.size(); i=i+2){
    checkBoxes.get(i).click();
    }
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    int checkedCount=0, uncheckedCount=0;
    for(int i=0; i<checkBoxes.size(); i++){
    System.out.println(i+" checkbox is selected "+checkBoxes.get(i).isSelected());
    if(checkBoxes.get(i).isSelected()){
                checkedCount++;
            }else{
                uncheckedCount++;
            }
        }
        System.out.println("number of selected checkbox: "+checkedCount);
        System.out.println("number of unselected checkbox: "+uncheckedCount);
    }

推荐答案

Vivek解决了这个问题,但是他的代码对我也不起作用.我对他的解决方案进行了一些修改,使其可以在chrome上运行.首先,我没有使用xpath,还添加了webdriver wait,以便我们在单击该元素之前先等待它.

Vivek's nailed the problem, however his code didn't work for me either. I made a few modifications to his solutions to make it work on chrome. First of all, I didn't use xpath and also I added webdriver wait so that we wait for the element before we click on it.

 WebDriver driver = new ChromeDriver();
 driver.get("http://www.samsung.com/us/video/tvs/all-products");
 int length = driver.findElements(By.name("filterOption")).size();
 WebDriverWait wait = new WebDriverWait(driver,300);
 for (int i = 0; i < length; i++) {
    WebElement ele = wait.until(ExpectedConditions.elementToBeClickable(driver.findElements(By.name("filterOption")).get(i)));
      if (!ele.isSelected())
           ele.click();
 }

这篇关于如何在Java中使用WebDriver选中多个复选框并进行验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆