0

I have a table of generating the dynamic checkboxes as below:

<div style="background: #fff;">
                    <div class="modal-body">
                        <table class="table no-margin">
                            <thead>
                                <tr>
                                    <th>Select</th>
                                    <th>Group Name</th>
                                    <th>Contacts</th>
                                </tr>
                            </thead>
                            <tbody>
                                
                                        <tr>
                                            <td>
                                                <div class="checkbox checkbox-styled">
                                                    <label>
                                                        <input type="checkbox" class="datacontact" onchange="SelectContacts(this);" data-listid='1' data-grpname='Newsletter' />
                                                    </label>
                                                </div>
                                            </td>
                                            <td>Newsletter</td>
                                            <td>0</td>
                                        </tr>
                                    
                                        <tr>
                                            <td>
                                                <div class="checkbox checkbox-styled">
                                                    <label>
                                                        <input type="checkbox" class="datacontact" onchange="SelectContacts(this);" data-listid='2' data-grpname='Website Leads Buyer' />
                                                    </label>
                                                </div>
                                            </td>
                                            <td>Website Leads Buyer</td>
                                            <td>0</td>
                                        </tr>
                                    
                                        <tr>
                                            <td>
                                                <div class="checkbox checkbox-styled">
                                                    <label>
                                                        <input type="checkbox" class="datacontact" onchange="SelectContacts(this);" data-listid='3' data-grpname='Website Leads Seller' />
                                                    </label>
                                                </div>
                                            </td>
                                            <td>Website Leads Seller</td>
                                            <td>2</td>
                                        </tr>
                                    
                                        <tr>
                                            <td>
                                                <div class="checkbox checkbox-styled">
                                                    <label>
                                                        <input type="checkbox" class="datacontact" onchange="SelectContacts(this);" data-listid='4' data-grpname='Website Leads Investor' />
                                                    </label>
                                                </div>
                                            </td>
                                            <td>Website Leads Investor</td>
                                            <td>0</td>
                                        </tr>
                                    
                                        <tr>
                                            <td>
                                                <div class="checkbox checkbox-styled">
                                                    <label>
                                                        <input type="checkbox" class="datacontact" onchange="SelectContacts(this);" data-listid='5' data-grpname='Website Leads' />
                                                    </label>
                                                </div>
                                            </td>
                                            <td>Website Leads</td>
                                            <td>0</td>
                                        </tr>
                                    
                                        <tr>
                                            <td>
                                                <div class="checkbox checkbox-styled">
                                                    <label>
                                                        <input type="checkbox" class="datacontact" onchange="SelectContacts(this);" data-listid='7980' data-grpname='NewGroup1' />
                                                    </label>
                                                </div>
                                            </td>
                                            <td>NewGroup1</td>
                                            <td>0</td>
                                        </tr>
                                    
                                        <tr>
                                            <td>
                                                <div class="checkbox checkbox-styled">
                                                    <label>
                                                        <input type="checkbox" class="datacontact" onchange="SelectContacts(this);" data-listid='7996' data-grpname='My Group' />
                                                    </label>
                                                </div>
                                            </td>
                                            <td>My Group</td>
                                            <td>0</td>
                                        </tr>
                                    
                                        <tr>
                                            <td>
                                                <div class="checkbox checkbox-styled">
                                                    <label>
                                                        <input type="checkbox" class="datacontact" onchange="SelectContacts(this);" data-listid='9136' data-grpname='Email Functionality ' />
                                                    </label>
                                                </div>
                                            </td>
                                            <td>Email Functionality </td>
                                            <td>0</td>
                                        </tr>
                                    
                            </tbody>
                        </table>                        
                    </div>
                    <div class="modal-footer">
                        <button type="button" data-dismiss="modal" class="btn btn-flat btn-default-dark ink-reaction">OK</button>
                        <button type="button" data-dismiss="modal" class="btn btn-flat btn-default-dark ink-reaction">Cancel</button>
                    </div>
                </div>
            </div>
        </div>
    </div>

And through the below mentiond code, I have getting value of the checkbox called "Website Leads Seller"

package TestScripts;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.WebElement;

//import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;

public class EmailClient_VerifyEmail {

    public static void main(String[] args) {
        System.setProperty("webdriver.gecko.driver", "C:/geckodriver/geckodriver-v0.21.0-win64/geckodriver.exe");

        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
        driver.get("http://cp.iagentsolutions.com/login.aspx");

        //enter email id
        driver.findElement(By.cssSelector("#txtuserName")).sendKeys("abdul123");

        //enter password
        driver.findElement(By.cssSelector("#txtPassword")).sendKeys("admin123");

        // click on login
        driver.findElement(By.cssSelector("#LoginSnd")).click();

        System.out.println("User Login Successfully from CP");

        //hover on menu
        //driver.findElement(By.xpath("html body.menubar-hoverable.header-fixed.menubar-visible form#form1 div#base div#menubar.menubar-inverse.animate div.nano.has-scrollbar div.nano-content div.menubar-scroll-panel ul#main-menu.gui-controls"))
        Actions action = new Actions(driver);

        //WebElement element = 
        action.moveToElement(driver.findElement(By.cssSelector(".menubar-foot-panel"))).perform();//("html body.menubar-hoverable.header-fixed.menubar-visible form#form1 div#base div#menubar.menubar-inverse.animate div.nano.has-scrollbar div.nano-content div.menubar-scroll-panel ul#main-menu.gui-controls")));

        //click on Email Client menu for open sub-menus
        driver.findElement(By.xpath("/html/body/form/div[3]/div[2]/div[2]/div[1]/div/ul/li[7]/a/span")).click();
        driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);        
        // click on Create Email
        driver.findElement(By.xpath("/html/body/form/div[3]/div[2]/div[2]/div[1]/div/ul/li[7]/ul/li[1]/a/span")).click();

        //Enter draft name
        driver.findElement(By.name("ctl00$body$txtDraftName")).sendKeys("Try to send without verify email");

        //Click on Next button
        driver.findElement(By.name("ctl00$body$btnStep1Next")).click();

        //Email Settings
        // Enter subject
        driver.findElement(By.id("body_txtSubject")).sendKeys("Try to send without verify email");
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

        //Click on Add / Edit to open Contact List
        driver.findElement(By.linkText("Add / Edit")).click();
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

        driver.findElements(By.xpath("//input[@type='checkbox' and @class='datacontact']")).forEach(elelemnt -> {
            elelemnt.click();
        });     

    }

}

But this code is only working for getting the check box title instead of clicking on the checkbox.

1 Answer 1

1

I have tested it and it is working properly : using below code :

driver.findElements(By.xpath("//input[@type='checkbox' and @class='datacontact']")).forEach(element -> {
        if (element.getAttribute("data-grpname").trim().equals("Website Leads Seller")) {
            ((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
        }
    });
Sign up to request clarification or add additional context in comments.

6 Comments

i have tried your code and it is working fine without any update.May be there is click issue that is not working.try to debug if the code is going in if condition or not.
i have added code to click using javascript. If that also not works then you can use Actions class to click on element.
When I am try to debug my script, I am getting this error, " The Source Not Found, The source attachment does not contain the source for the file RemoteWebDriver.class.
the code that you have put here does not have any problem and it is somewhere else . please update your post with whole class so that we can identify the problem. Also on which line are you getting this error. You can provide error stack trace.
I have updated my post and i getting the error at this line "driver.findElements(By.xpath("//input[@type='checkbox' and @class='datacontact']")).forEach(elelemnt -> {"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.