How To Check CheckBox From Multiple CheckBoxes Using DataProvider In Selenium WebDriver With Java?

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 Checkbox = driver.findElements(By.xpath("//input[@type='checkbox']"));
 
 for(int i=0; i
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:

Replies

  • Ankita Katdare
    Ankita Katdare
    Tagging #-Link-Snipped-# #-Link-Snipped-# Please tag anyone who may be familiar with the issue.
  • simplycoder
    simplycoder
    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 Checkbox
    • The case of String id =El.getAttribute("value"); is same as the one passed in EventCategory.
  • Radhika Deshpande
    Radhika Deshpande
    #-Link-Snipped-# 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
    simplycoder
    Radhika
    #-Link-Snipped-# 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
    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)..
  • Radhika Deshpande
    Radhika Deshpande
    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. }
    #-Link-Snipped-# 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 #-Link-Snipped-# 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
    Radhika Deshpande
    Hello ,

    I tried below piece and it worked :

    List Checkbox = driver.findElements(By.xpath("//input[@type='checkbox']"));
            int i=0;
            while(i
                                        
  • Saandeep Sreerambatla
    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.
  • Radhika Deshpande
    Radhika Deshpande
    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
    simplycoder
    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
    Amar Khavashi

    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
    Radhika Deshpande

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

You are reading an archived discussion.

Related Posts

LG Electronics (LG) has planned to preview advanced robot technologies at CES 2017, to showcase its innovations in artificial intelligence. AI is expected to be a major theme of CES...
Samsung is resisting the urge to make new smartphones and instead focusing on revamping its existing range with every New Year. Just like it did before, it has announced upgraded...
A new smartphone device from Asus, X00GD has been spotted online on a Chinese Certification Site, TENNA recently. The smartphone is listed to be featuring a 5.2-inch HD (720 x...
Solar panel, battery & inverter required for 1500W load?
Good evening engineers , actually I m really confused I poured 10 cm pcc layer last week and now I want to isolate the pcc layer by rubberised bitumen emulison...