How to fill out a web form through c#

Asked

Viewed 2,209 times

0

Is there any way to fill out a web form via c#?

I have a system developed in WPF and when the user clicks a button open a particular page in the browser and your fields are filled automatically with the system information.

I managed to sort it out like this:

WPF:

<Window x:Class="PreencherFormularioWeb.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <WebBrowser Name="webBrowerNavegacao" LoadCompleted="webBrowerNavegacao_LoadCompleted"/> 
</Grid>

C#:

//Necessario adicionar referencia no projeto.
using mshtml;


namespace PreencherFormularioWeb
{

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        //Adiciona o caminho URL para abrir a pagina dentro do controle webBroser
        webBrowerNavegacao.Navigate(new Uri("https://accounts.google.com")); 
    }

    //Variavel usada para entrar no evento somente a primeira vez.
    //Se não ele entra no evento novamente quando ocorre um post.
    bool JaEntreiNoEvento = false;

    //Entra no evento quando o webBroser conclui o carregamento da pagina
    private void webBrowerNavegacao_LoadCompleted(object sender, NavigationEventArgs e)
    {
        if (JaEntreiNoEvento == false)
        {
            //Recupera o document do webBrowser1, faz o cast para mshtml.HTMLDocument para ter acesso aos elementos da pagina
            mshtml.HTMLDocument htmldoc;
            htmldoc = webBrowerNavegacao.Document as mshtml.HTMLDocument;

            //Recupera o elemento pelo id e adiciona a string.
            htmldoc.getElementById("Email").innerText = "Teste";
            htmldoc.getElementById("Passwd").innerText = "123";

            //Recupera o botão e aciona.
            htmldoc.getElementById("signIn").click();

            JaEntreiNoEvento = true;
        }    
    }
}

}

  • I don’t understand anything you want.

  • The question has the tags winforms and wpf, but none of these technologies are used for web pages...

  • dcastro, Serve yes, managed to do, Webbrowser control opens HTML pages inside winforms and wpf

1 answer

2


If the question is exactly what I understand, you can try to do it this way:

Winform

public Form1()
    {
        InitializeComponent();
        //navigate to you destination 
        webBrowser1.Navigate("https://www.certiport.com/portal/SSL/Login.aspx");
    }
    bool is_sec_page = false;
    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        if (!is_sec_page)
        {
            //get page element with id
            webBrowser1.Document.GetElementById("c_Username").InnerText = "username";
            webBrowser1.Document.GetElementById("c_Password").InnerText = "pass";
            //login in to account(fire a login button promagatelly)
            webBrowser1.Document.GetElementById("c_LoginBtn_c_CommandBtn").InvokeMember("click");
            is_sec_page = true;
        }
        //secound page(if correctly aotanticate
        else
        {
            //intract with sec page elements with theire ids and so on
        }

    }

WPF

public MainWindow()
    {
        InitializeComponent();
 webBrowser1.Navigate(new Uri("https://www.certiport.com/portal/SSL/Login.aspx"));
        }
        bool is_sec_page = false;
        mshtml.HTMLDocument htmldoc;
        private void webBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
        {
            htmldoc = webBrowser1.Document as mshtml.HTMLDocument;
            if (!is_sec_page)
            {
                //get page element with id
                htmldoc.getElementById("c_Username").innerText = "username";
                //or
                //htmldoc.getElementById("c_Username")..SetAttribute("value", "username");
                htmldoc.getElementById("c_Password").innerText = "pass";
                //login in to account(fire a login button promagatelly)
                htmldoc.getElementById("c_LoginBtn_c_CommandBtn").InvokeMember("click");
                is_sec_page = true;
            }
            //secound page(if correctly aotanticate
            else
            {
                //intract with sec page elements with theire ids and so on
            }
        }

Note: I did not test the code

That reply was copied from ONLY

  • 1

    I managed to do but Invokemember is not found, so I solved another way, the code is on top. Thanks.

Browser other questions tagged

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