Webbrowser - Click on elements of a site

2

Hello, everyone!

It's this, I need to make my program click on an element. Here is a photo to make it easier to understand: link

I have tried many things, but nothing successful so far ... I want the button to be pressed

My code is like this so far:

Dim PageElement1 As HtmlElementCollection = WebBrowser1.Document.All.GetElementsByName("")
    For Each CurElement1 As HtmlElement In PageElement1

        If (CurElement1.GetAttribute("classname") = "preferred-login facebook-login") Then

            CurElement1.InvokeMember("click")
            MsgBox("Deu Certo")
        End If

    Next

But the "click" does not work ... Well, if anyone can help ... Thank you in advance!

    
asked by anonymous 20.07.2015 / 23:39

3 answers

2

You can get the element by the class and click on it without having to iterate all elements of the page.

webBrowser1.FindElementByClassName("preferred-login facebook-login").Click();

Or by using the By.ClassName selector:

webBrowser1.FindElement(By.ClassName("preferred-login facebook-login")).Click();
    
21.07.2015 / 11:55
1

Using the code:

WebBrowser1.Document.All.GetElementsByName("")

You are getting all the elements of the page that have the name attribute of "" . That is:

<div id="id" name="bobagem"></div> <!-- não encontrado -->
<div name=""></div> <!-- encontrado! -->
<div name="" id="id2"></div> <!-- encontrado! -->
    
21.07.2015 / 03:50
1

I've been trying to solve the same dilemma you faced, or still face, and luckily, by making a small modification to your code, I was able to click the button.

Obs1: I had to fill the GetElementsByName("COLOQUEI O NAME DO BOTÂO ALVO") .

Obs2: I had to remove the If ... , and it worked.

Dim PageElement1 As HtmlElementCollection = WebBrowser1.Document.All.GetElementsByName("COLOCAR O NAME DO BOTÃO ALVO")
For Each CurElement1 As HtmlElement In PageElement1

        CurElement1.InvokeMember("click")
        MsgBox("Deu Certo")

Next

This way it worked for me, I hope it will be useful to you. A hug and thank you.

    
04.09.2016 / 05:58