java复选框web,如何在Java中使用WebDriver选择多个复选框并进行验证?

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 checkBoxes =driver.findElements(By.xpath("//input[@name='filterOption']"));

for(int i=0; i

checkBoxes.get(i).click();

}

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

int checkedCount=0, uncheckedCount=0;

for(int i=0; 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'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();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值