From the Archives
Archived discussions on CrazyEngineers

@Radhika Deshpande • 01 Jan, 2017 • 1 like
Problem : How To Check CheckBox From Multiple CheckBoxes On WebPage Using Data Driven FrameWork(Excel) In Selenium WebDriver with JAVA.

Scenario : I am currently automating a form where I am passing all input from excel. It includes textboxes ,checkboxes datepicker etc.
Form includes total 15 checkboxes. Now I want my script to check 1 checkbox which is mentioned in excel. I am using TestNG and Apache POI for it.
Here is what I have done to get it :

public void EventCreation1(String EventName, String EventTagLine,String EventOrganization,String City,String State,String Country, String EventCategory ,String EventDescription,String Name,String EmailID,String Contact)
 {
 
 
 driver.get("WEBPAGE URL");
 
 driver.findElement(By.xpath(".//*[@id='eventName']")).sendKeys(EventName);
 driver.findElement(By.xpath(".//*[@id='eventTagLine']")).sendKeys(EventTagLine);
 driver.findElement(By.xpath(".//*[@id='organizerName']")).sendKeys(EventOrganization);
 driver.findElement(By.xpath(".//*[@id='eventCity']")).sendKeys(City);
 driver.findElement(By.xpath(".//*[@id='eventState']")).sendKeys(State);
 driver.findElement(By.xpath(".//*[@id='eventCountry']")).sendKeys(Country);
//For Category
List<WebElement> Checkbox = driver.findElements(By.xpath("//input[@type='checkbox']"));
 
 for(int i=0; i<Checkbox.size();i++ )
 
 {
 WebElement El =Checkbox.get(i);
 String id =El.getAttribute("value");
 if(id.equals(EventCategory))
 {
 El.click();
 
 }
 }

Here // String EventCategory is the Checkbox option I am passing as parameter from excel.

Could you please help in this what is wrong here? I am not able to check any checkbox with this.

HTML Tag:
<div class="section colm colm9">
    <div class="frm-row">
        <div class="section colm colm4">
            <div class="option-group">
                <label class="option option-blue block">
                    <input type="checkbox" name="eventType[]" value="TechFest">
                    <span class="checkbox"></span> TechFest
                </label>
                <label class="option option-blue block spacer-t10">
                    <input type="checkbox" name="eventType[]" value="Cultural Fest">
                    <span class="checkbox"></span> Cultural Fest
                </label>
                <label class="option option-blue block spacer-t10">
                    <input type="checkbox" name="eventType[]" value="Management Fest">
                    <span class="checkbox"></span> Management Fest
                </label>
                <label class="option option-blue block spacer-t10">
                    <input type="checkbox" name="eventType[]" value="Sports Fest">
                    <span class="checkbox"></span> Sports Fest
                </label>
                <label class="option option-blue block spacer-t10">
                    <input type="checkbox" name="eventType[]" value="Workshop">
                    <span class="checkbox"></span> Workshop
                </label>
            </div><!-- end .option-group section -->
        </div>
    </div>
</div>
@Ankita Katdare • 02 Jan, 2017 Tagging @Saandeep Sreerambatla @Anil Jain Please tag anyone who may be familiar with the issue.
@simplycoder • 05 Jan, 2017 • 1 like I do not have selenium set up with Java here, but just to clarify,
  • Are you getting the correct number of check boxes in the list List<WebElement> Checkbox
  • The case of String id =El.getAttribute("value"); is same as the one passed in EventCategory.
@Radhika Deshpande • 06 Jan, 2017 @simplycoder Thnaks for looking into it.
I am getting correct number of checkboxes in List .i.e 15 .
But problem is in Web element El I am getting all checkboxes values and then with
if(id.equals(EventCategory) it compares all values with one value in 'EventCategory' and fails.
Do I need iterate a list of WebElements and then compare it with EventCategory and then perform action of Click() ?
@simplycoder • 07 Jan, 2017
Radhika
@simplycoder Thnaks for looking into it.
I am getting correct number of checkboxes in List .i.e 15 .
But problem is in Web element El I am getting all checkboxes values and then with
if(id.equals(EventCategory) it compares all values with one value in 'EventCategory' and fails.
Do I need iterate a list of WebElements and then compare it with EventCategory and then perform action of Click() ?
I think you are correct in a way.
I would try this if I were you
  1. Get the list of check boxes (You already done that)
  2. Iterate through the list of check boxes with value attribute
  3. try something like this
String id = Checkbox.get(i).getAttribute("value");
 if(id.equals(EventCategory))
 {
Checkbox.get(i).click();
// break if its only going to be one value.
 }
Does this give you the correct output?
The code above needs to be optimized ( Calling .get(i) twice...).

If not let me know.
@Saandeep Sreerambatla • 10 Jan, 2017 • 1 like I think what gave above is correct, can you also perform trim as well. ( I think you might have done it already though)..
@Radhika Deshpande • 10 Jan, 2017
simplycoder
String id = Checkbox.get(i).getAttribute("value"); if(id.equals(EventCategory)) { Checkbox.get(i).click(); // break if its only going to be one value. }
@simplycoder I tried this piece of code but it is not working. Do I need to use Iterator() method or ListIterator() method in java to iterate through the list of check boxes with value attribute?


Saandeep Sreerambatla
I think what gave above is correct, can you also perform trim as well. ( I think you might have done it already though)..
Thanks @Saandeep Sreerambatla for looking into it. I have not used trim() function as of now. I am JAVA beginner, thus not sure of what all function should be suitable for which condition. I will try in my code and let you know.
@Radhika Deshpande • 11 Jan, 2017 • 2 likes Hello ,

I tried below piece and it worked :

List<WebElement> Checkbox = driver.findElements(By.xpath("//input[@type='checkbox']"));
        int i=0;
        while(i<Checkbox.size())
        {
            WebElement El =Checkbox.get(i);   
            String id =El.getAttribute("value");
            driver.manage().timeouts().implicitlyWait(5000, TimeUnit.SECONDS);
            if(id.equals(EventCategory))
            {
                Checkbox.get(i).click();
                break;
            }
            i++;
            }//End While
@Saandeep Sreerambatla • 12 Jan, 2017 • 1 like So the wait statement did the trick!
So always use it whenever something doesnt work, it helps. And Instead of using something like as implicit wait create something for explicit wait , I mean a user defined wait function to wait for appropriate times.
@Radhika Deshpande • 12 Jan, 2017
Saandeep Sreerambatla
So the wait statement did the trick!
So always use it whenever something doesnt work, it helps. And Instead of using something like as implicit wait create something for explicit wait , I mean a user defined wait function to wait for appropriate times.
Thanks a lot, i will make a note of it onwards 😀
@simplycoder • 14 Jan, 2017 • 1 like
Saandeep Sreerambatla
So the wait statement did the trick!
So always use it whenever something doesnt work, it helps. And Instead of using something like as implicit wait create something for explicit wait , I mean a user defined wait function to wait for appropriate times.
Issue with this code, is the hard-coded 5000 seconds.
Ideally it would wait till the element is loaded.
In this way you can maintain log of loading times of all controls on UI.
But this doesn't seem to be with the context of the question asked.
@Amar Khavashi • 17 Sep, 2018 • 1 like

Hi radhika, nice tutorial.. I m facing issues in selecting values from excel file to select drop down  values , please can you show me complete detail example

@Radhika Deshpande • 01 Oct, 2018

Hi Amar, Where exactly the  problem you are facing? I am happy to help if you put your sample code. 

Related Posts

Archives

@Kaustubh Katdare · May 8, 2015

Chandrayaan-2, India's ambitious second mission to the moon has been scheduled to take off sometime in 2017, the Indian Parliament was informed yesterday. Jitendra Singh, the minister of state in...
Archives

@Ankita Katdare · Sep 14, 2011

I have heard HCL Conducts 'Technical GD' 2 persons say a technical topic and get time to think for 10 mins and then candidates are asked to discuss it. Topics...
Archives

@Ankita Katdare · Aug 11, 2011

There is very little information about the campus selection process of Tata Elxsi on the internet. If someone has appeared for their interview, please share your experience. I have got...
Archives

@Dhananjay Harkare · Mar 28, 2014

After launching its much awaited One M8, HTC now has come up with Dual-SIM Desire 310 in India. With good design, quad-core processor and average camera features, HTC has added...
Archives

@Handsome hari · Jan 17, 2013

PLEASE SUGGEST ME..RELATED TO CIVIL ENGINEERING, WHICH ADDITIONAL COURSE CAN I CHOOSE THAT IS USEFUL FOR CIVIL?