Click here to Skip to main content
15,921,774 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
var links = GeckoWB.Document.GetElementsByTagName("span");
            foreach (var link in links)
            {
                if (link.GetAttribute("ipsNotificationCount ipsHide") == "data-currentcount")
                {
                    
                }
            }
Posted
Updated 14-Feb-17 3:16am
v2
Comments
madhav_jain 14-Feb-17 7:22am    
what u have tried and describe more about it.
xXxRevolutionxXx 14-Feb-17 7:29am    
I updated the question.

1 solution

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:
C#
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:
C#
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))
        {
            ...
        }
    }
}
 
Share this answer
 
Comments
xXxRevolutionxXx 14-Feb-17 11:22am    
One correction to your code. The "var link" in the foreach loop needs to be type of: GeckoHtmlElement or else... using the var data type, it becomes type of GeckoNode, and doesn't include a "GetAttribute" method. Now the thing with your code, is that it returns me an "Object reference not set to an instance of an object." error... because the 'links' variable is null, and it doesn't contain any "elements". Here is my code:

http://prntscr.com/e8oo33

This is the "crash" error: http://prntscr.com/e8oq15

P.S I use the method invoker thing, because Gecko Browser is in another thread.
Richard Deeming 14-Feb-17 12:19pm    
That's not even remotely surprising. The BeginInvoke method returns immediately. The code that follows it will execute before the delegate you've passed to BeginInvoke.

Move the entire code of the method inside the BeginInvoke delegate, or make the entire method run on the UI thread:
private void CountMsgAndNot()
{
    if (InvokeRequired)
    {
        BeginInvoke((Action)CountMsgAndNot);
        return;
    }
    
    var links = GeckoWB.Document.GetElementsByClassName("ipsNotificationCount");
    foreach (GeckoHtmlElement link in links)
    {
        string value = link.GetAttribute("data-currentcount");
        if (!string.IsNullOrWhiteSpace(value))
        {
            MsgLbl.Text = value;
            break;
        }
    }
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900