[C#] [gecko]从span类中获取文本 [英] [C#][gecko]get text from span class
问题描述
[ ^ ]
我尝试了几种方法来拍摄这张照片数据,但似乎没有任何工作....谷歌这是它可以得到我。有任何想法吗 ?提前致谢。
我尝试了什么:
我尝试过获取所有类元素并循环遍历它们以及其他一些东西。
var links = GeckoWB.Document.GetElementsByTagName( span);
foreach ( var link in 链接)
{
if (link.GetAttribute( ipsNotificationCount ipsHide)== data-currentcount )
{
}
}
传递给 GetAttribute [ ^ ]方法是您要检索的属性的名称。 />
您传递了名为class
的属性的值。那样不行。没有这样的属性,并且永远不会有,因为属性名称不能包含空格。
您可以将您的测试更改为:
if (link.GetAttribute( class)== ipsNotificationCount ipsHide)
{
...
}
但这样效率低,如果班级顺序可能会破坏名称已更改,或者另一个类已添加到元素中。
相反,请使用 GetElementsByClassName [ ^ ]查找具有指定类的元素的方法:
VAR 温泉n> links = GeckoWB.Document.GetElementsByClassName( ipsNotificationCount);
foreach ( var link in 链接)
{
string value = link.GetAttribute( data-currentcount);
if (!string.IsNullOrWhiteSpace( value ))
{
int currentCount;
if ( int .TryParse( value , out currentCount))
{
...
}
}
}
Hello ! So, i am creating a project using Gecko(firefox) browser written in C#. I am stuck at a specific thing. I want to get the text of a span class, which doesn't have an ID. Here is the code:
[^]
I tried several ways to take this piece of data, but nothing seems work.... and google this is as far as it could get me. Any ideas ? Thanks in advance.
What I have tried:
I tried to get all class elements and loop through them, and some other stuff.
var links = GeckoWB.Document.GetElementsByTagName("span");
foreach (var link in links)
{
if (link.GetAttribute("ipsNotificationCount ipsHide") == "data-currentcount")
{
}
}
The string you pass to the GetAttribute[^] method is the name of the attribute you want to retrieve.
You are passing in the value of the attribute calledclass
. That will not work. There is no such attribute, and there never can be, because attribute names cannot contain spaces.
You could change your test to:
if (link.GetAttribute("class") == "ipsNotificationCount ipsHide") { ... }
But that would be inefficient, and likely to break if the order of the class names changed, or another class was added to the element.
Instead, use the GetElementsByClassName[^] method to find the element(s) with the specified class:
var links = GeckoWB.Document.GetElementsByClassName("ipsNotificationCount"); foreach (var link in links) { string value = link.GetAttribute("data-currentcount"); if (!string.IsNullOrWhiteSpace(value)) { int currentCount; if (int.TryParse(value, out currentCount)) { ... } } }
这篇关于[C#] [gecko]从span类中获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!