kunal soni kunal soni - 4 months ago 14
Java Question

i need to click on check box named sign up for newsletter using selenium webdriver in java

I need to click on check box named sign up for newsletter using selenium webdriver in java

<div class="input-box">
<input type="checkbox" name="is_subscribed" title="Sign Up for Newsletter" value="1" id="is_subscribed" class="checkbox"><label for="is_subscribed"><span></span>Sign Up for Newsletter</label>
</div>

Answer

Target the checkbox element using findelements method and xpath locator and store it in a list as below and use the for loop to traverse through the list and if condition to check whether the checkbox is selected and perform select using click.

List <WebElement> checkbox = driver.findElements(By.xpath("Your xpath here"));

for(WebElement e:checkbox)
{
if(e.getAttribute("value").equals("1")){
e.click;
}
}

Or as below

 List <WebElement> checkbox = driver.findElements(By.xpath("Your xpath here"));

for(WebElement e:checkbox)
{
if(!e.isSelected){
e.click;
}
}
Comments