Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Click button in webbrowser

I am using this code:

Code
Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
        For Each webpageelement As HtmlElement In allelements
            If webpageelement.InnerHtml = "Ok" Then
                webpageelement.InvokeMember("click")
                Exit For
            End If
        Next


It loops through the page to find any button with Ok in it, and then clicks it.

I only want it to click the 3rd button it loops through.

How do I apply this?
[Image: MbYfMNk.png]
i prefer using the modulus when iterating through a list looking for an nth item

using the mod operator:
Code
if(i % 3 == 0)
{
    //every third item
    //do something then break
}
(Jun 10, 2017 07:11 PM)fxbghacker Wrote:  i prefer using the modulus when iterating through a list looking for an nth item

using the mod operator:
Code
if(i % 3 == 0)
{
    //every third item
    //do something then break
}

Okey so how do i apply this to my existing code?
[Image: MbYfMNk.png]
I would put it in a while loop. The alignment of the code might look weird. Also, my code might not be the most efficient. I am a little rusty in VB.NET.

Code
Dim i As Integer = 0

while i < 3
        Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
           For Each webpageelement As HtmlElement In allelements
               If webpageelement.InnerHtml = "Ok" Then
                  webpageelement.InvokeMember("click")
                  i += 1
                  Exit For
              End If
        Next
End While

Edit:

I think I misunderstood your question. Did you want it to press the first three buttons? Or only the third button? If it is the third button, then why not look it up by ID and click it through the ID name?
add a placeholder integet, if you want every third item, make it 3

Code
itemCounter = 3
counter = 0

so something like this maybe? (my vb is super rusty)

Code
If webpageelement.InnerHtml = "Ok" And modComp = itemCounter
(Jun 10, 2017 07:20 PM)CoderEX Wrote:  I would put it in a while loop. The alignment of the code might look weird. Also, my code might not be the most efficient. I am a little rusty in VB.NET.

Code
Dim i As Integer = 0

while i < 3
        Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
           For Each webpageelement As HtmlElement In allelements
               If webpageelement.InnerHtml = "Ok" Then
                  webpageelement.InvokeMember("click")
                  i += 1
                  Exit For
              End If
        Next
End While

Edit:

I think I misunderstood your question. Did you want it to press the first three buttons? Or only the third button? If it is the third button, then why not look it up by ID and click it through the ID name?

Only the third button, I can't look it up by id since its not there.
[Image: MbYfMNk.png]
(Jun 10, 2017 07:11 PM)fxbghacker Wrote:  i prefer using the modulus when iterating through a list looking for an nth item

using the mod operator:
Code
if(i % 3 == 0)
{
    //every third item
    //do something then break
}

Why not use a for loop and then use step?
(Jun 11, 2017 09:42 PM)Ninja Wrote:  Why not use a for loop and then use step?

the if statement is inside the loop (i guess i didnt make that clear), im not familiar with 'step', like i said ive been out of VB for quite some time (barely used it alot before either). i like the feel of c# more :p

i would personally use a regex instead of looping, and then call the click method of the button (or form action) but meh, to each their own
Here's the update:
If u can provide the webpage link you working on i'll give you more optimized code

Code
Dim btnCount As Interger = 0
Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
        For Each webpageelement As HtmlElement In allelements
            btnCount += 1
            If webpageelement.InnerHtml = "Ok" And btnCount = 3 Then
                webpageelement.InvokeMember("click")
                Exit For
            End If
        Next
(Jun 12, 2017 03:02 PM)fxbghacker Wrote:  the if statement is inside the loop (i guess i didnt make that clear)

So..?
Thanks to all of you, I managed to work it out myself :)
[Image: MbYfMNk.png]
(Jun 14, 2017 08:40 AM)Ninja Wrote:  So..?

so for example

Code
//add some data to the list
for(int i=0;i<22;i++)
{
    listBox1.Items.Add("item " + i.ToString());
}

//get every third item
for (int i = 0; i < listBox1.Items.Count; i++)
{
    //the if inside the loop
    if (i % 3 == 0 && i != 0)
    {
        MessageBox.Show("this is the third item: item " + i.ToString());
        listBox1.Items.RemoveAt(i);
    }
}

every third item


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by The Pulp FictionApr 24, 2022 12:22 AM 2 199 Apr 24, 2022 12:22 AM
Last Post: The Pulp Fiction
[.NET] by triplextremeSep 15, 2020 02:29 PM 7 223 Sep 15, 2020 02:29 PM
Last Post: Dredd
[.NET] by requiredhqMay 27, 2020 08:38 AM 2 105 May 27, 2020 08:38 AM
Last Post: Yui Hirasawa