Search code examples
javacheckboxselenium-webdrivertestngtestng-eclipse

Can't make selenium webdriver click the checkbox


I'm working on eclipse neon, selenium webdriver 2.53.0 and firefox 45.3.0 esr. I can't make selenium mark the only checkbox on the site. Here is my test script:

package testy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class rejestr {
    public WebDriver driver;
    @BeforeMethod
    public void beforeMethod() {
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.navigate().to("http://dev.wedkarz.pzw.pl/#/login");
    }
    @Test
    public void f () throws InterruptedException { 
        driver.findElement(By.className("nav-item")).click();
        driver.findElement(By.id("firstName")).sendKeys("Jan");
        driver.findElement(By.id("lastName")).sendKeys("Nowak");
        driver.findElement(By.id("email")).sendKeys("jnowak@o2.pl"); 
        driver.findElement(By.id("plainPassword")).sendKeys("password");
        new Select(driver.findElement(By.id("district"))).selectByVisibleText("Okręg     PZW w Bydgoszczy");
        new Select(driver.findElement(By.id("circle"))).selectByVisibleText("Koło PZW nr 31");
        driver.findElement(By.id("fishingLicense")).sendKeys("12345678990");
        driver.findElement(By.id("squaredOne")).click();
    }
    @AfterMethod
    public void afterMethod() {
        System.out.print("Test zakończony powodzeniem");
        driver.quit();
    }
}

And that's html code for checkbox:

<div class="row form-group m-b-lg">
              <div class="squaredOne">
                <input id="squaredOne" name="check" type="checkbox" value="None" class="ng-untouched ng-valid ng-dirty">
                <label for="squaredOne">
                  <p>Wyrażam zgodę na otrzymywanie od PZW informacji<br> o charakterze promocyjnym i reklamowym
                  przekazywanych drogą elektroniczną, w tym przesyłanych z wykorzystaniem telekomunikacyjnych
                  urządzeń końcowych (np. komputer, tablet).</p>
                </label>
              </div>
              <span class="help-block">

              </span>
            </div>

Would really appreciate Your help, because I've been fighting with it for 2 days.


Solution

  • If you are on IE browser its a little tricky -

    if (driver.Capabilities.BrowserName.Equals(“internet explorer"))
        driver.findElement(By.id("squaredOne").SendKeys(Keys.Space);
    else
        driver.findElement(By.id("squaredOne").Click();
    

    Or try using Xpath to reach the element

    driver.findElement(By.xpath("//input[@type='checkbox']")).click();
    

    If that doesn't work, try using class name

    WebElement mybox = driver.findElement(By.className("squaredOne"));     
    mybox.click();