简体   繁体   中英

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.

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

Your code driver.findElement(By.id("squaredOne")).click() should work. But if it doesn't, then you can try:

  1. Change selector to xpath:

     driver.findElement(By.xpath("//input[@id='squaredOne']")).click(); 
  2. Click parent div:

     driver.findElement(By.xpath("//div[./input[@id='squaredOne']]")).click()` 
  3. Use Actions class:

     Actions actions = new Actions(driver); actions.click(driver.findElement(By.xpath("//input[@id='squaredOne']"))); actions.build().perform(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM