如何使用C#使用Selenium将所有元素放入列表或字符串中? [英] How to get all elements into list or string with Selenium using C#?

查看:377
本文介绍了如何使用C#使用Selenium将所有元素放入列表或字符串中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过使用WebElement(和IWebElement)和字符串列表,但是我一直在遇到错误.如何获得按类名列出的所有元素的列表或字符串?我有所有硒参考.我需要一些java.util dll吗?

I've tried using WebElement (and IWebElement) and string lists, but I keep getting errors. How can I get a list or strings of all the elements text by classname? I have all Selenium references. Do I need some java.util dll?

我应该实现一个foreach循环吗?

Should I implement a foreach loop?

IList<IWebElement> all = new IList<IWebElement>();
all = driver.FindElements(By.ClassName("comments")).Text;

错误:无法创建抽象类或接口'System.Collections.Generic.IList'的实例

Error: Cannot create an instance of the abstract class or interface 'System.Collections.Generic.IList'

错误:"System.Collections.ObjectModel.ReadOnlyCollection"不包含文本"的定义,也没有扩展方法文本"接受第一个类型的参数

Error: 'System.Collections.ObjectModel.ReadOnlyCollection' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type

推荐答案

您可以像这样获得所有元素文本:

You can get all of the element text like this:

IList<IWebElement> all = driver.FindElements(By.ClassName("comments"));

String[] allText = new String[all.Count];
int i = 0;
foreach (IWebElement element in all)
{
    allText[i++] = element.Text;
}

这篇关于如何使用C#使用Selenium将所有元素放入列表或字符串中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆