【问题标题】:Open webbrowser, auto complete form components and submit打开浏览器,自动完成表单组件并提交
【发布时间】:2013-03-05 01:55:14
【问题描述】:

我们目前正在研究一种创建 WPF/winforms 应用程序的方法,我们可以在内部设置该应用程序:-

  1. 自动打开一个新的网络浏览器实例到一个预定义的 URL
  2. 使用预定义数据自动填写必填字段
  3. 自动提交表单并等待下一页加载
  4. 使用预定义数据自动填写必填字段(第 2 页)
  5. 自动提交表单并等待下一页加载(等)

经过大量调查,我们唯一能找到的是通过以下方式打开网络浏览器:-

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);

任何关于如何完成该过程的建议/阅读建议都将不胜感激。

【问题讨论】:

    标签: c# .net wpf winforms browser


    【解决方案1】:

    我写了一个在 html 页面中填充元素的示例。你必须这样做:

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

    只需导航到特定的 URL 并填充页面元素。

    【讨论】:

    • 感谢您的快速回复。但是,这使用了 winform 上的内置浏览器控件,而不是打开浏览器的新实例。这是实现此目的的唯一方法(推荐方法?)
    • @user2009091:你正在使用 wpf?
    • 我们可以使用 / 或 .. 目前这是我们正在努力工作的概念证明
    • 谢谢。但是,这仍然依赖于使用浏览器 .net 控件。而实际上并没有打开资源管理器。这是唯一的方法吗?
    • @user2009091:trye using SHDocVw.dll 有一篇文章:codeproject.com/Articles/43491/…
    【解决方案2】:

    如果我理解正确,您想在网络浏览器中打开一些 URL,然后像普通用户一样与网站交互。对于此类任务,我建议您查看Selenium。虽然它通常用作回归测试自动化工具,但没有人可以阻止您将其用作浏览器自动化工具。

    Selenium 有详细的documentation 和大的community。很可能您会想要使用Selenium WebDriver,它可以通过nuget 获得。

    下面是一个典型的 Selenium“脚本”的小例子(取自文档):

    // 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();
    

    我个人建议根据用户操作(注册、登录、填写表单、在网格中选择内容、过滤网格等)来思考和组织脚本。这将为脚本提供良好的形状和可读性,而不是杂乱的硬编码代码块。本例中的脚本可能类似于:

    // 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);
    

    【讨论】:

    • 太棒了!几天来我一直在寻找这种类型的解决方案。
    • 很好的答案!谢谢!
    【解决方案3】:
    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");
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-07-14
      • 2018-12-11
      • 1970-01-01
      • 2017-04-20
      • 2011-04-21
      • 2012-01-12
      • 1970-01-01
      • 2017-09-10
      • 2012-07-11
      相关资源
      最近更新 更多