[C#] [gecko]从span类中获取文本 [英] [C#][gecko]get text from span class

查看:188
本文介绍了[C#] [gecko]从span类中获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!所以,我正在使用用C#编写的Gecko(firefox)浏览器创建一个项目。我被困在一个特定的事情上。我想获得没有ID的span类的文本。以下是代码:



[ ^ ]



我尝试了几种方法来拍摄这张照片数据,但似乎没有任何工作....谷歌这是它可以得到我。有任何想法吗 ?提前致谢。



我尝试了什么:



我尝试过获取所有类元素并循环遍历它们以及其他一些东西。



  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  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 called class. 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屋!

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