Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用windows窗体登录基于javascript的站点?_C#_Javascript - Fatal编程技术网

C# 如何使用windows窗体登录基于javascript的站点?

C# 如何使用windows窗体登录基于javascript的站点?,c#,javascript,C#,Javascript,我一直试图通过使用GetElementByTag/GetElemenByName方法,使用WebBrowser控件自动登录到一个站点,但没有太大成功。(WFA-C#) 我相信主要的原因是这个网站是用JavaScript编写的 我做了一些研究,发现了两种方法: 通过模仿网站登录表单,我可以使用一种叫做POST的东西。(或类似的东西) 将javascript输入注入站点中的所有输入字段 我不知道如何解决这个问题,而且由于我完全缺乏javaScript或任何基于web的编程经验,我真的可以对手头的问题

我一直试图通过使用
GetElementByTag
/
GetElemenByName
方法,使用WebBrowser控件自动登录到一个站点,但没有太大成功。(WFA-C#)

我相信主要的原因是这个网站是用JavaScript编写的

我做了一些研究,发现了两种方法:

  • 通过模仿网站登录表单,我可以使用一种叫做POST的东西。(或类似的东西)
  • 将javascript输入注入站点中的所有输入字段
  • 我不知道如何解决这个问题,而且由于我完全缺乏javaScript或任何基于web的编程经验,我真的可以对手头的问题使用任何建议或解决方案

    /**编辑1 谢谢你的快速回复。 我试图使用你的代码,但仍然面临相同的异常抛出

    代码如下: (我稍微修剪了一下)

    我在上面的用户名行上得到了一个“System.nullReferenceException”


    我试图访问的站点是-“…也许我的方法错了?!”

    您可以使用web浏览器控件,只需在目标页面上找到元素id并填充它们即可

    我给你写了一个简单的例子:

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