Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
var ele = (
                from T in webBrowser1.Document.GetElementsByTagName("div").Cast<HtmlElement>()
                where T.GetAttribute("classname") == "ball ball-1"
                select T).FirstOrDefault();


Hey, im trying to make a simple csgodouble bot, with a ui. I want to get some data from a website called, csgodouble. but i have came into a problem.

div data-rollid="310835" class="ball ball-1">1 /div


html element.
but there are 10 lines there look like that, with very small different, and they change each minute or so. how can you only get the data from the 10th line??
have tried to use the code above, but it will read all the 10th line.

i know xpath, will provide you to only get data from 10th line. but have no clue how to use it, to get that data in my application. i use the windowsapplication project in visual studio.
//*[@id="past"]/div[10] xpath code.

i have searched all over google for help, cant seem to find anything!

If have know how to fix it, and will help. please say what you have done, and why. so i can get a better understanding of c# and code in general.




update to the question d 5/18-2016 - 9:51

C#
http://imgur.com/a/7VdHB

Hey here can you see my code, and the source code from csgodouble.

I have tried everything to get the 10th line information.
have tried ElementAtOrDefault(9); at 10, instead of 9.. but doesnt seem to work
the data-rollid change every time a new number comes in.

i want to get output, if it was red, black or green there won.
the last line in the source code, is color that won
ball ball-1 = red
ball ball-8 = black
ball ball-0 = green</pre>


What I have tried:

C#
var ele = (
                from T in webBrowser1.Document.GetElementsByTagName("div").Cast<HtmlElement>()
                where T.GetAttribute("classname") == "ball ball-1[10]"
                select T).FirstOrDefault();


And other simpler code, but i have delete them. tried a little bit of xpath. but not much.
Posted
Updated 17-May-16 21:52pm
v5

1 solution

If you want to get the 10th item from the sequence, replace FirstOrDefault with ElementAtOrDefault[^]:
C#
var parent = webBrowser1.Document.GetElementById("past");
if (parent != null)
{
    var balls = from b in parent.GetElementsByTagName("div").Cast<HtmlElement>()
                where b.GetAttribute("classname").StartsWith("ball ball-")
                select b;
    
    var theBall = balls.ElementAtOrDefault(9);
    if (theBall != null)
    {
        // Do something with the element here...
    }
}

NB: The index is zero-based, so the 10th element is at index 9.
 
Share this answer
 
v3
Comments
Member 12527869 17-May-16 9:36am    
Well theirs website is down right now, i will get back, when it comes up. i hope i work!!
Member 12527869 18-May-16 2:53am    
Hey, i have tried it, and i cant get it to work, it does not found the 10th line, have tried different ways to use ElementAtDefault. but cant seem it to work. have you another idea?
Richard Deeming 18-May-16 7:08am    
Try splitting the part that extracts the elements on to its own line:

var elements = from T in webBrowser1.Document.GetElementsByTagName("div").Cast<HtmlElement>()
   where T.GetAttribute("classname") == "ball ball-1"
   select T;

var ele = elements.ElementAtOrDefault(9);


Then, set a breakpoint on the second line, and inspect the results in the elements variable to make sure you've got at least 10 items.

If you haven't got at least 10 items, then you'll need to look at the document to see where you've gone wrong.

As a guess, I would suggest that not all of the elements have the CSS class ball ball-1; the "ball-1" part is probably a sequential number.
Member 12527869 18-May-16 8:34am    
i havnt used the debug mode before, so i have no clue what to look after, it returns a null. Am im right if i said, that i doesnt work when it returns a null??

i tried the debug mode another part of my code, that i know works. but it shows the same thing..

Yeah the ball ball-1, doesnt work, beacuse it a class, and it not every time that there are 10 lines with only ball ball-1 classes.

i have done it with (div) (id) past, as shown in my edit of the main question.
Richard Deeming 18-May-16 8:47am    
If you don't have 10 elements where the class name is equal to "ball ball-1", then you can't find the 10th element matching that condition!

Try looking for elements where the class name starts with "ball ball-". You can also restrict the search to elements with the specific parent <div> - see my updated solution.

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