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();
}