使用 C# 的 Selenium WebDriver 中的项目列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16818950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
List of items in Selenium WebDriver using C#
提问by jessica
How to write the same piece of code in C# ?
如何在 C# 中编写相同的代码?
List<WebElement> displayedOptions = driver.FindElements(By.TagName("select"));
for (WebElement option : displayedOptions)
{
if (option.displayed)
{
displayedOptions.Add(option);
}
}
采纳答案by Arran
There is a class created specifically for selectHTML elements (i.e. dropdowns).
有一个专门为selectHTML 元素(即下拉菜单)创建的类。
It is the SelectElementclass inside the OpenQA.Selenium.Support.UInamespace.
它是OpenQA.Selenium.Support.UI命名空间中的SelectElement类。
This is a wrapper around selectelements, giving easy access to common things people use/interact with in selectelements.
这是select元素的包装器,可以轻松访问人们在select元素中使用/交互的常见事物。
Your example would be translated into (using C# 3 or above, since I'm using LINQ):
您的示例将被转换为(使用 C# 3 或更高版本,因为我使用的是 LINQ):
IList<IWebElement> selectElements = driver.FindElements(By.TagName("select"));
var displayedSelectElements = selectElements.Where(se => se.Displayed);
It's important to know what this code does. It will first find allselectelements and put them into a new list.
了解这段代码的作用很重要。它将首先找到所有select元素并将它们放入一个新列表中。
It will then filter those out, to only the selectelements that are displayed, that is, their .Displayedproperty is true. This is a direct translation of your example code.
然后它会将这些过滤掉,只select显示显示的元素,即它们的.Displayed属性为true。这是您的示例代码的直接翻译。
However, you've not really specified what you are trying to do, and I think the example is better suited as this:
但是,您并没有真正指定您要做什么,我认为这个例子更适合这样:
var selectElement = new SelectElement(driver.FindElement(By.Id("something")));
var displayedOptions = selectElement.Options.Where(o => o.Displayed);
The above would find a specificselectelements, and filter the options withinthat selectto only those that are displayed. Again, that is, have their .Displayedproperty as true.
上面会找到一个特定的select元素,过滤器的选项之内即select只显示那些。同样,也就是说,将它们的.Displayed属性设为true。
Edit
编辑
Since the above code is what you need, but you want in the form of a forloop, a similar thing would look like:
由于上面的代码是你所需要的,但你想要for循环的形式,类似的事情看起来像:
var selectElement = new SelectElement(driver.FindElement(By.Id("something")));
var allOptions = selectElement.Options;
for (int i = 0; i < allOptions.Length; i++)
{
if (allOptions[i].Displayed)
{
// do something
// like add to a new list?
}
}
回答by gtzinos
FindElements returns ReadOnlyCollection. So its better to define
FindElements 返回 ReadOnlyCollection。所以最好定义
ReadOnlyCollection<IWebElement> displayedOptions = driver.FindElements(By.TagName("select"));
for (WebElement option : displayedOptions)
{
if (option.displayed)
{
//displayedOptions.Add(option); //You can't do that
// do something else
}
}
回答by Dmitri
If your SVG data looks like this :
如果您的 SVG 数据如下所示:
<div id="content-data" class="col-md-12" style="height:666px;">
<svg id="pie-draw-488172" width="492" height="616"><g transform="translate(226,318)">
<text id="pie-text" cursor="default" y="-226" x="241">Compute (227,311)</text>
<text id="pie-text" cursor="default" y="-211" x="241">Database (98,005)</text>
<text id="pie-text" cursor="default" y="-196" x="241">Storage&Content Delivery (69,436)</text>
<text id="pie-text" cursor="default" y="-181" x="241">Networking (30,874)</text>
<text id="pie-text" cursor="default" y="-166" x="241">Other (11,273)</text>
</svg>
</div>
then use this
然后用这个
public List<IWebElement> content_data = new List<IWebElement>();
content_data = driver.FindElement(By.Id("content-data")).FindElements(By.TagName("text")).ToList();

