How to navigate to another page using Webbrowser?

Asked

Viewed 231 times

1

I am trying to access a URL and send login and password for authentication and then browse between pages?

After I log in to the page http://indigo.rafson.com.br, I’m not getting switched to another page that would be http://indigo.rafson.com.br/01.php

webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.Navigate(Url);
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete && webBrowser1.Document == null)
{
    Application.DoEvents();
}

HtmlDocument doc = webBrowser1.Document;
HtmlElement login = doc.GetElementById("login");
HtmlElement password = doc.GetElementById("Password");
HtmlElement submit = doc.GetElementById("submit");
login.SetAttribute("value", Login);
password.SetAttribute("value", Senha);
submit.InvokeMember("click");

webBrowser1.Navigate("http://indigo.rafson.com.br/01.php");
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete && webBrowser1.Document == null)
{
    Application.DoEvents();
}
  • Why can’t you change? What happens when you try to change?

1 answer

2


Look for actions through the event Documentcompleted. This event is triggered whenever a navigation is completed.

You can use this in conjunction with a Action, so that a method can be defined to be executed whenever a navigation is completed.

Below is an example of how to define the event Documentcompleted and the methods to be performed after a navigation has been completed.

public class Form1
{
    // Recebe a url de navegação.
    private string url = string.Empty;

    private string Login = "login";

    private string Senha = "Senha";

    // Define o próximo método a ser executado.
    private Action NextStap { get; set; }

    public void Form1()
    {
        InitializeComponent();

        // Define a url a ser navegada.
        url = "http://site.com.br";

        // Define o próximo método a ser executado.
        NextStap = Stap1;

        // Define um método para ser executado após o termino do carregamento de uma página.
        webBrowser1.DocumentCompleted += WebBrowser_DocumentCompleted;

        // Aciona a navegação.
        // Após o termino, será acionado o método WebBrowser_DocumentCompleted.
        webBrowser1.Navigate(new Uri(url));
    }

    private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        // Executa o método definido na variável NextStap.
        NextStap();
    }

    public void Stap1()
    {
        HtmlDocument doc = webBrowser1.Document;
        HtmlElement usernamelogin = doc.GetElementById("login");
        HtmlElement password = doc.GetElementById("Password");
        HtmlElement submit = doc.GetElementById("submit");
        usernamelogin.SetAttribute("value", Login);
        password.SetAttribute("value", Senha);

        // Definimos aqui o próximo método a ser executado
        // após uma nova navegação ser completada.
        NextStap = Stap2;

        // A ação abaixo provavelmente irá aguardar algum retorno do navegador.
        // Após sua execução o Stap2 deverá ser acionado.
        submit.InvokeMember("click");
    }

    public void Stap2()
    {
        webBrowser1.Navigate("http://indigo.rafson.com.br/01.php");
    }
}
  • Can I open the url in a browser and pass login and password to the screen ? and then press the button ?

  • Yes, that can be done.

Browser other questions tagged

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