12

現在、内部でセットアップできる WPF/winforms アプリケーションを作成する方法を調査しています:-

  1. Web ブラウザーの新しいインスタンスを事前定義された URL に自動的に開く
  2. 必須フィールドに事前定義されたデータを自動的に入力します
  3. フォームを自動的に送信し、次のページがロードされるのを待ちます
  4. 事前定義されたデータで必須フィールドを自動的に入力 (ページ 2)
  5. フォームを自動的に送信し、次のページが読み込まれるのを待ちます (など)

多くの調査の後、私たちが見つけることができた唯一のことは、次の方法でWebブラウザーを開くことです:-

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

プロセスを完了する方法について、アドバイス/推奨事項を読んでいただければ幸いです。

4

3 に答える 3

24

HTMLページの要素を埋める例を書きました。次のようにする必要があります。

ウィンフォーム

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 に移動してページ要素を埋めるだけです。

于 2013-03-16T10:01:47.020 に答える
7

私があなたの言うことを正しく理解していれば、Web ブラウザーで URL を開いて、通常のユーザーと同じようにサイトと対話したいと思うでしょう。そのようなタスクについては、 Seleniumを参照することをお勧めします。通常、回帰テストの自動化ツールとして使用されますが、ブラウザー自動化ツールとしての使用を止めることはできません。

Selenium には詳細なドキュメントと大きなコミュニティがあります。ほとんどの場合、 nugetから入手できるSelenium WebDriverを使用したいと思うでしょう。

以下は、典型的な 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);
于 2013-03-17T19:17:22.197 に答える
2
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");
    }
  }
}
于 2015-12-18T01:30:34.363 に答える