Checkbox is not selected in selenium java. Tried everything but nothing worked

69 Views Asked by At

I'm trying to fill in registration page for this the user will first be on the sign up page where they have to enter name and email address and then click on Signup button. This will navigate to the registration page where user will have to enter all the details. On registration page the checkboxes are not clickable. Kindly help me with this. This is POM class

public class RegisterNewUserPom {
    
    private WebDriver driver;
    private By newName= By.name("name");
    private By newEmail= By.xpath("//form[@action='/signup']/input[3]");
    private By signup=By.xpath("//form[@action='/signup']/button");
    private By title=By.id("id_gender1");
    private By password=By.id("password");
    private By days=By.id("days");
    private By month=By.id("months");
    private By year=By.id("years");
    private By newsLetter=By.id("newsletter");
    private By optional=By.xpath("optin");
    private By firstName=By.id("first_name");
    private By lastName=By.id("last_name");
    private By company=By.id("company");
    private By address1=By.id("address1");
    private By address2= By.id("address2");
    private By country=By.id("country");
    private By state=By.id("state");
    private By city= By.id("city");
    private By zipcode= By.id("zipcode");
    private By mobileNumber=By.id("mobile_number");

    
    public RegisterNewUserPom(WebDriver driver) {
        this.driver=driver;
    }
    
    public void EnterNewName(String name) {
        driver.findElement(newName).sendKeys(name);
    }
    public void EnterNewEmail(String email) {
        driver.findElement(newEmail).sendKeys(email);
        
    }
    public void ClickSignUpButton() {
        Actions actions=new Actions(driver);
        actions.moveToElement(driver.findElement(signup)).click().build().perform();
    }
    
    public void SelectTitle() {
        driver.findElement(title).click();
        
    }
    
    public void EnterPassword(String passWord) {
        driver.findElement(password).sendKeys(passWord);
    }
    public void SelectDay() {
        WebElement day=driver.findElement(days);
        Select select=new Select(day);
        select.selectByValue("2");
    }
    public void SelectMonth() {
        WebElement months=driver.findElement(month);
        Select select=new Select(months);
        select.selectByVisibleText("June");
    }
    public void SelectYear() {
        WebElement years=driver.findElement(year);
        Select select=new Select(years);
        select.selectByValue("1992");
    }
    
    public void SelectCheckbox() {
        driver.findElement(newsLetter).click();
        driver.findElement(optional).click();
    }
    
    public void EnterAddress(String add1, String add2) {
        driver.findElement(address1).sendKeys(add1);
        driver.findElement(address2).sendKeys(add2);
        
    }
    public void SelectCountry() {
        WebElement countries=driver.findElement(country);
        Select select=new Select(countries);
        select.selectByValue("India");
    }
    public void EnterFirstName(String fname) {
        driver.findElement(firstName).sendKeys(fname);
        
    }
    
    public void EnterLastName(String lname) {
        driver.findElement(lastName).sendKeys(lname);
        
    }
    
    public void EnterCompanyName(String cname) {
        driver.findElement(company).sendKeys(cname);
        
    }
    
    public void EnterCity(String cityName) {
        driver.findElement(city).sendKeys(cityName);
    }
    public void EnterState(String stateName) {
        driver.findElement(state).sendKeys(stateName);
    }
    
    public void EnterZipCode(int zip) {
        String zipString= Integer.toString(zip);
        driver.findElement(zipcode).sendKeys(zipString);
    }
    
    public void EnterMobileNumber(int mnumber) {
        String mobile= Integer.toString(mnumber);
        driver.findElement(mobileNumber).sendKeys(mobile);
    }
    public void RegisterUser(String name, String email) {
        EnterNewName(name);
        EnterNewEmail(email);
        ClickSignUpButton();        
    }
    public void EnterUserDetail(String pass, String fname, String lname,
            String companyName, String address1, String address2,
            String state, String city, int zipcode, int  mobile) {
        SelectTitle();
        EnterPassword(pass);
        SelectDay();
        SelectMonth();
        SelectYear();
        SelectCheckbox();
        EnterFirstName(fname);
        EnterLastName(lname);
        EnterCompanyName(companyName);
        EnterAddress(address1, address2);
        SelectCountry();
        EnterState(state);
        EnterCity(city);
        EnterZipCode(zipcode);
        EnterMobileNumber(mobile);
        
        
        
    }
}

This is Test class

public class RegisterTest {
    WebDriver driver;
    LoginPOM loginPom;
    RegisterNewUserPom registernewuser;
    Random randomGenerator = new Random();  
    int randomInt = randomGenerator.nextInt(1000);  
    
    @BeforeMethod
    public void setup() {
        driver= new ChromeDriver();
        driver.get("http://automationexercise.com");
        driver.findElement(By.xpath("//div[@class='shop-menu pull-right']/ul/li[4]/a")).click();
        registernewuser= new RegisterNewUserPom(driver);        
    }
    @Test
    public void RegisterNewUser() {
        String randomEmail="username"+ randomInt +"@testpractice.com";
        registernewuser.RegisterUser("Rohit", randomEmail);
    }
    
    @AfterTest
    public void EnterDetailsInForm() {
        String password="Test@123";
        String fname="Rohit";
        String lname= "Tester";
        String company="test company";
        String add1="hello";
        String add2="hello2222";
        String state="myState";
        String city="dream city";
        int zipcode=1234;
        int mnumber=1234567890;
        registernewuser.EnterUserDetail(password, fname, lname, company, add1, add2, state, city, zipcode, mnumber);
    }
}

This is HTML code for checkboxes

<div class="checker" id="uniform-newsletter"><span class=""><input type="checkbox" name="newsletter" id="newsletter" value="1"></span></div>

<label for="newsletter">Sign up for our newsletter!</label>
<div class="checker" id="uniform-optin"><span><input type="checkbox" name="optin" id="optin" value="1"></span></div>

<label for="optin">Receive special offers from our partners!</label>

This is the code which I'm trying to run but everytime I run this code then on the register page the checkbox are not clickable and I get error for element click intercepted. I tried with all the possible method like js executor, actions, webdriver wait but nothing works. Please let me know how to click the checkbox

I have tried with webdriver wait on the checkbox till element to be clickable, tried with jsexecutor as well and actions as well but nothing worked. And I don't know why it is not clickable even though id id is given.

Error message when tried all the possible ways:

org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <label for="newsletter">...</label> is not clickable at point (222, 658). Other element would receive the click: <iframe id="aswift_6" name="aswift_6" style="width: 1005px !important; height: 124px !important; display: block; margin: 0px auto;" sandbox="allow-forms allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-top-navigation-by-user-activation" width="1005" height="124" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" src="https://googleads.g.doubleclick.net/pagead/html/r20230420/r20110914/zrt_lookup.html?fsb=1#RS-1-&amp;adk=1812271801&amp;client=ca-pub-1677597403311019&amp;fa=1&amp;ifi=7&amp;uci=a!7&amp;btvi=4&amp;xpc=5L11JsaZOU&amp;p=https%3A//automationexercise.com" data-google-container-id="a!7" data-google-query-id="CPiC_bC8xP4CFVVkYAodURkGgw" data-load-complete="true"></iframe>
1

There are 1 best solutions below

0
On

It's very wierd that the label can not be located.

I've managed to make the click while iterating all labels.

Code example:

package tests;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import selenium.ChromeDriverSetup;

public class AutomationexerciseRegister extends ChromeDriverSetup {

    public static void main(String[] args) {
        
        WebDriver driver = startChromeDriver();
        driver.get("http://automationexercise.com");
        driver.findElement(By.xpath("//div[@class='shop-menu pull-right']/ul/li[4]/a")).click();
        
        String userName = "testuser_" + timestampOfPattern("yyyyMMdd_HHmmss");
        String email = userName + "@mailinator.com";
        WebElement inputName = driver.findElement(By.name("name"));
        inputName.click();
        inputName.sendKeys(userName);
        
        
        WebElement inputEmail = driver.findElements(By.name("email")).get(1);
        inputEmail.click();
        inputEmail.sendKeys(email);
        driver.findElement(By.xpath("//form[@action='/signup']/button")).click();
        
        
        List<WebElement> labels = driver.findElements(By.tagName("label"));
        clickLabelFor(labels, "newsletter");
        clickLabelFor(labels, "optin");
       
        driver.quit(); // breakpoint here to see the checkboxes

    }
    
    public static String timestampOfPattern(String pattern) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return now.format(dateTimeFormatter);
    }
    
    public static void clickLabelFor(List<WebElement> labels, String attrFor) {
        for (WebElement label: labels) {
            String forValue = label.getAttribute("for");
            if (forValue != null) {
                if (forValue.equals(attrFor)) {
                    label.click();
                    break;
                }
            }
        }
    }

}