使用Selenium Webdriver获取div中的项目列表 [英] Getting list of items inside div using Selenium Webdriver
本文介绍了使用Selenium Webdriver获取div中的项目列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有以下
<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
如何访问div class =facetContainerDiv中的所有元素?
How do I access all elements inside the div class="facetContainerDiv" ?
我试过
List<WebElements> elementsList = driver.findElements(By.cssSelector(".facetContainerDiv"));
for(WebElement checkBox:elementsList) {
int i=0;
checkBox = elementsList.get(i);
bla bla bla..
}
在上面的代码中elementList只有一个元素,其中type为null。
in the above code elementsList has only one element with "type" as null.
推荐答案
请按照下面的代码与您的案例完全匹配。
Follow the code below exactly matched with your case.
- 为div下的div创建一个web元素的界面, class 为 facetContainerDiv
- Create an interface of the web element for the div under div with class as facetContainerDiv
即
<div class="facetContainerDiv">
<div>
</div>
</div>
2。创建一个包含第二个div内所有元素的IList,即
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。使用索引访问每个复选框
3. Access each check boxes using the index
请找到以下代码
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();
}
}
}
这篇关于使用Selenium Webdriver获取div中的项目列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文