How to fill a web form through C #

0

Is there any way to populate a web form through C #?

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

I was able to solve 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;
        }    
    }
}

}

    
asked by anonymous 09.04.2015 / 21:56

1 answer

2

If the question is exactly what I understood, 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

  

This response has been copied from SO

    
10.04.2015 / 12:58