Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting list of items inside div using Selenium Webdriver

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.

like image 312
Damien-Amen Avatar asked Mar 21 '13 01:03

Damien-Amen


2 Answers

Follow the code below exactly matched with your case.

  1. 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(); 

        }
    }
}
like image 108
CheryJose Avatar answered Sep 24 '22 17:09

CheryJose


I'm not sure if your findElements statement gets you all the divs. I would try the following:

List<WebElement> elementsRoot = driver.findElements(By.xpath("//div[@class=\"facetContainerDiv\"]/div));

for(int i = 0; i < elementsRoot.size(); ++i) {
     WebElement checkbox = elementsRoot.get(i).findElement(By.xpath("./label/input"));
     checkbox.click();
     blah blah blah
}

The idea here is that you get the root element then use another a 'sub' xpath or any selector you like to find the node element. Of course the xpath or selector may need to be adjusted to properly find the element you want.

like image 29
so cal cheesehead Avatar answered Sep 24 '22 17:09

so cal cheesehead