Hack Forums
[.NET] Click button in webbrowser - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Click button in webbrowser (/showthread.php?tid=5643906)



Click button in webbrowser - Johan Cruyff - Jun 10, 2017

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?


* - fxbghacker - Jun 10, 2017

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
}



* - Johan Cruyff - Jun 10, 2017

(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?


* - CoderEX - Jun 10, 2017

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?


* - fxbghacker - Jun 10, 2017

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



* - Johan Cruyff - Jun 11, 2017

(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.


* - Ninja - Jun 11, 2017

(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?


* - fxbghacker - Jun 12, 2017

(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


* - iphone5s - Jun 14, 2017

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



* - Ninja - Jun 14, 2017

(Jun 12, 2017 03:02 PM)fxbghacker Wrote:  the if statement is inside the loop (i guess i didnt make that clear)

So..?


* - Johan Cruyff - Jun 14, 2017

Thanks to all of you, I managed to work it out myself :)


* - fxbghacker - Jun 14, 2017

(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