Getting list of items inside div using Selenium Webdriver

匿名 (未验证) 提交于 2019-12-03 07:36:14

问题:

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.

回答1:

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();           }     } }


回答2:

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.



回答3:

You're asking for all the elements of class facetContainerDiv, of which there is only one (your outer-most div). Why not do

List<WebElement> checks =  driver.findElements(By.class("facetCheck")); // click the 3rd checkbox checks.get(2).click();


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!