Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open webbrowser, auto complete form components and submit

We are currently investigating a method of creating a WPF/winforms application that we can set up internally to :-

  1. automatically open a new instance of a web browser to a predefined URL
  2. automatically complete required fields with predefined data
  3. automatically submit form and wait for the next page to load
  4. automatically complete required fields with predefined data (page 2)
  5. automatically submit form and wait for the next page to load (etc)

after much investigation, the only thing we have managed to find is the opening up of a web browser via :-

object o = null;

SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer();
IWebBrowserApp wb = (IWebBrowserApp)ie;
wb.Visible = true;
wb.Navigate(url, ref o, ref o, ref o, ref o);

Any advice / reading recommendations would be appreciated on how to complete the process.

like image 456
Simon Avatar asked Mar 16 '13 09:03

Simon


2 Answers

I wrote an example for filling in an element in a html page. You must do something like this:

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
                }
            }

Just navigate to specific URL and fill page element.

like image 81
KF2 Avatar answered Oct 31 '22 11:10

KF2


If I understood you right you want to open some URL in web browser and then interact with site as a normal user would. For such task I can suggest taking look at Selenium. While it is typically used as a regression testing automation tool no one can stop you from using it as a browser automation tool.

Selenium has detailed documentation and big community. Most probably you will want to use Selenium WebDriver which is available via nuget.

Below is a little example of typical Selenium "script" (taken as-is from documentation):

// Create a new instance of the Firefox driver.

// Notice that the remainder of the code relies on the interface, 
// not the implementation.

// Further note that other drivers (InternetExplorerDriver,
// ChromeDriver, etc.) will require further configuration 
// before this example will work. See the wiki pages for the
// individual drivers at http://code.google.com/p/selenium/wiki
// for further information.
IWebDriver driver = new FirefoxDriver();

//Notice navigation is slightly different than the Java version
//This is because 'get' is a keyword in C#
driver.Navigate().GoToUrl("http://www.google.com/");

// Find the text input element by its name
IWebElement query = driver.FindElement(By.Name("q"));

// Enter something to search for
query.SendKeys("Cheese");

// Now submit the form. WebDriver will find the form for us from the element
query.Submit();

// Google's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 10 seconds
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until((d) => { return d.Title.ToLower().StartsWith("cheese"); });

// Should see: "Cheese - Google Search"
System.Console.WriteLine("Page title is: " + driver.Title);

//Close the browser
driver.Quit();

Personally I can suggest to think and organize scripts in terms of user actions (register, login, fill form, select something in grid, filter grid, etc.). This will give a good shape and readability to scripts instead of messy hardcoded code chunks. The script in this case can look similar to this:

// Fill username and password
// Click on button "login"
// Wait until page got loaded
LoginAs("[email protected]", "johndoepasswd"); 

// Follow link in navigation menu
GotoPage(Pages.Reports); 

// Fill inputs to reflect year-to-date filter
// Click on filter button
// Wait until page refreshes
ReportsView.FilterBy(ReportsView.Filters.YTD(2012)); 

// Output value of Total row from grid
Console.WriteLine(ReportsView.Grid.Total);
like image 44
Alexander Manekovskiy Avatar answered Oct 31 '22 11:10

Alexander Manekovskiy