Webbrowser - Click on elements of a website

Asked

Viewed 1,130 times

2

Hello, everybody!

Here’s the thing, I need to make my program click on an element. Here’s a photo to make it easier to understand: http://i.imgur.com/Uitnkvt.png

I’ve tried a lot of things, but nothing successful so far... I want the button 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" doesn’t work... Well, if anyone can help... Thank you in advance!

3 answers

2

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

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

Or using the By.Classname selector:

webBrowser1.FindElement(By.ClassName("preferred-login facebook-login")).Click();

1

Using the code:

WebBrowser1.Document.All.GetElementsByName("")

You’re taking all the elements from the page that has the name attribute of "". That is to say:

<div id="id" name="bobagem"></div> <!-- não encontrado -->
<div name=""></div> <!-- encontrado! -->
<div name="" id="id2"></div> <!-- encontrado! -->
  • So guys, I tried in many ways, but the problem is not finding the element, the problem is even clicking it. It is not clicking... Someone can try to make an application that clicks the facebook login button of the site en.stackoverflow and send me an example for me to study?

  • @Wesleysilva dude, you can also make Selenium run a Javascript command that click the button for you.

1

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

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

Note: I had to remove the If ..., and then 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.