Getting list of items inside div using Selenium Webdriver

java, selenium, selenium-webdriver

Solution

Follow the code below exactly matched with your case.

- Create an interface of the web element for the div under div with class as facetContainerDiv

ie for

<div class="facetContainerDiv">
    <div>

    </div>
</div>

2. Create an IList with all the elements inside the second div i.e for,

<label class="facetLabel">
   <input class="facetCheck" type="checkbox" />
</label>
<label class="facetLabel">
   <input class="facetCheck" type="checkbox" />
</label>
<label class="facetLabel">
   <input class="facetCheck" type="checkbox" />
</label>
<label class="facetLabel">
   <input class="facetCheck" type="checkbox" />
</label>
<label class="facetLabel">
   <input class="facetCheck" type="checkbox" />
</label>

3. Access each check boxes using the index

Please find the code below

using System;
using System.Collections.Generic;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace SeleniumTests
{
  class ChechBoxClickWthIndex
    {
        static void Main(string[] args)
        {

            IWebDriver driver = new FirefoxDriver();

            driver.Navigate().GoToUrl("file:///C:/Users/chery/Desktop/CheckBox.html");

            // Create an interface WebElement of the div under div with **class as facetContainerDiv**
            IWebElement WebElement =    driver.FindElement(By.XPath("//div[@class='facetContainerDiv']/div"));
            // Create an IList and intialize it with all the elements of div under div with **class as facetContainerDiv**
            IList<IWebElement> AllCheckBoxes = WebElement.FindElements(By.XPath("//label/input"));
            int RowCount = AllCheckBoxes.Count;
            for (int i = 0; i < RowCount; i++)
            {
            // Check the check boxes based on index
               AllCheckBoxes[i].Click();

            }
            Console.WriteLine(RowCount);
            Console.ReadLine(); 

        }
    }
}

Problem

Say I have the following ``` <div class="facetContainerDiv"> <div> <label class="facetLabel"> <input class="facetCheck" type="checkbox" /> </label> <label class="facetLabel"> <input class="facetCheck" type="checkbox" /> </label> <label class="facetLabel"> <input class="facetCheck" type="checkbox" /> </label> <label class="facetLabel"> <input class="facetCheck" type="checkbox" /> </label> <label class="facetLabel"> <input class="facetCheck" type="checkbox" /> </label> </div> </div> ``` Now I want to put a check mark on the checkbox based on the index I provide. So I write a method like below How do I access all elements inside the div class="facetContainerDiv" ? I tried ``` List<WebElements> elementsList = driver.findElements(By.cssSelector(".facetContainerDiv")); for(WebElement checkBox:elementsList) { int i=0; checkBox = elementsList.get(i); bla bla bla.. } ``` in the above code elementsList has only one element with "type" as null.

Original source