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.
– pnet
The question has the tags
winforms
andwpf
, but none of these technologies are used for web pages...– dcastro
dcastro, Serve yes, managed to do, Webbrowser control opens HTML pages inside winforms and wpf
– Mauricio Ferraz