Open a web browser, automatically fill out the form and submit

We are currently studying a method for creating a WPF / winforms application that we can configure internally: -

  • automatically opens a new instance of the web browser for the predefined URL
  • automatically fills in required fields with predefined data
  • automatically submit the form and wait for the next page to load.
  • automatically fills in the required fields with predefined data (page 2)
  • automatically submit the form and wait for the next page to load (etc.)

after a big investigation, the only thing we managed to find was opening a web browser through: -

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 recommendations for recommendations / reading will be appreciated as completing the process.

+6
source share
3 answers

I wrote an example to populate an element on an html page. You should 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 go to a specific URL and fill out the page element.

+16
source

If I understood correctly, you want to open some URL in a web browser, and then interact with the site as usual. For such a task, I can offer a look at Selenium . Although it is commonly used as a regression test automation tool, no one can stop you from using it as a browser automation tool.

Selenium described the documentation in detail and the large community. Most likely, you will want to use Selenium WebDriver , which is available through nuget .

Below is a small example of a typical selenium "script" (taken as is from the 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 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 thinking and organizing scripts in terms of user actions (register, login, fill out a form, select something in the grid, filter grid, etc.). This will give good form and readability for scripts instead of messy hard-coded code fragments. The script in this case might look something like this:

 // Fill username and password // Click on button "login" // Wait until page got loaded LoginAs(" johndoe@domain.com ", "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); 
+5
source
 if (webBrowser1.Document != null) { HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("input"); foreach (HtmlElement elem in elems) { String nameStr = elem.GetAttribute("name"); if (nameStr == "email") { webBrowser1.Document.GetElementById(nameStr).SetAttribute("value", " test_email@mail.com "); } } } 
+1
source

All Articles