[英]Get every ID from HTML-Code which contains certain Css-Value
Im trying to get every ID from HTML-Code which contains certain Css-Value and write them into a List.我试图从包含某些 Css 值的 HTML 代码中获取每个 ID 并将它们写入列表。
I already can get the first ID by using:我已经可以使用以下方法获取第一个 ID:
List<string> MyList = new List<string>();
int Count = driver.FindElements(By.TagName("My_Value")).Count;
for(int i=0; i<Count; i++)
{
string ID = driver.FindElement(By.CssSelector("div.My_Value")).GetAttribute("id");
MyList.Add(ID);
}
The problem is, that i always get the first ID from the HTML-Code althoug MyValue exist in different ID´s.问题是,我总是从 HTML 代码中获取第一个 ID,尽管 MyValue 存在于不同的 ID 中。 So how can i tell the programm to 'Jump' the allready checked Values?.那么我怎样才能告诉程序“跳转”已经检查的值呢?
The code looks fine, but it is not running a loop, as you say it is finding the 1st element that matches your criteria, then is complete.代码看起来不错,但它没有运行循环,正如您所说,它正在查找符合您的条件的第一个元素,然后完成。 To get all of them, try:要获得所有这些,请尝试:
string ID = driver.FindElements(By.CssSelector("div.My_Value")).GetAttribute("id");
Not tried this myself, but give it a go我自己没试过,但给它一个 go
If you are constructing the list using By.TagName("My_Value")
only the <My_Value>
elements will be collected and put into the list, ie如果您使用By.TagName("My_Value")
构建列表,则只会收集<My_Value>
元素并将其放入列表中,即
<My_Value>class="foo bar"...</My_Value>
Moving forward, within the loop FindElement(By.CssSelector("div.My_Value"))
identifies only the <div>
elements with class attribute as My_Value
ie向前看,在循环内FindElement(By.CssSelector("div.My_Value"))
仅标识具有class属性为My_Value
的<div>
元素,即
<div>class="My_Value"...</div>
So the outer FindElements()
and innerloop FindElement()
doesn't match.所以外部FindElements()
和内部循环FindElement()
不匹配。 Hence you don't get all the values.因此,您不会获得所有值。
To iterate over all the collected list elements you need to use:要遍历所有收集的列表元素,您需要使用:
string ID = driver.FindElement(By.TagName("My_Value")).GetAttribute("id");
I figured it out.我想到了。
You have to use a var variable to safe all the element´s.您必须使用 var 变量来保护所有元素。 And get the ID´s through a foreach Loop.并通过 foreach 循环获取 ID。
List<string> MyList = new List<string>();
var ID = driver.FindElement(By.CssSelector("div.My_Value"));
foreach(IWebElement element in ID)
{
element.GetAttribute("id")
MyList.Add(ID);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.