【问题标题】:c# Win Form - Unable to change text of an "input" in WebBrowser controlc# Win Form - 无法更改 WebBrowser 控件中“输入”的文本
【发布时间】:2018-01-30 09:28:57
【问题描述】:

我正在使用Webbrowser 控件通过以下代码登录此page

webBrowser1.Navigate(new Uri("https://services.gst.gov.in/services/login"));

现在要更改密码,我正在使用下面的代码,但它不起作用。

webBrowser1.Document.GetElementById("user_pass").InnerText = "ABC123";
//OR
webBrowser1.Document.GetElementById("user_pass").SetAttribute("type", "text");
webBrowser1.Document.GetElementById("user_pass").SetAttribute("text", "ABC123");
//OR
webBrowser1.Document.GetElementById("user_pass").SetAttribute("text", "ABC123");
//OR
webBrowser1.Document.GetElementById("user_pass").SetAttribute("value", "ABC123");

谁能告诉我怎么做?

【问题讨论】:

    标签: c# html .net winforms webbrowser-control


    【解决方案1】:

    您尝试设置的字段是密码类型字段,而不是文本类型字段。这不是一个好方法,但我建议你这样做:

    webBrowser1.Document.GetElementById("user_pass").SetAttribute("value", "ABC123");
    

    【讨论】:

    • 感谢您的回答,但是设置“value”属性的代码也不起作用。
    【解决方案2】:

    您首先必须等待页面完全加载

    webBrowser1.DocumentCompleted += OnDocumentLoaded;
    webBrowser1.Navigate(new Uri("https://services.gst.gov.in/services/login"));
    

    现在可以设置value属性(包含<input>元素的内容):

    private void OnDocumentLoaded(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        webBrowser1.Document.GetElementById("user_pass")
            .SetAttribute("value", "the_password");    
    }
    

    您也可以发送击键:

        webBrowser1.Document.GetElementById("user_pass").Focus();
        SendKeys.Send("the_password");
    

    【讨论】:

    • 感谢您的回答,但我已经采取措施,例如等待它完全加载并且设置“value”属性的代码也不起作用,我已经尝试发送不起作用的密钥.
    • 首先确保您使用的 ID 是正确的,并且该文档已加载(您示例中的 URL 根本不起作用)
    • 请你检查一下我是否犯了任何错误,因为我已经检查过了,它似乎没问题,我在看到它已完全加载后手动触发按钮点击代码。跨度>
    • 网址services.gst.gov.in/services/login 正确吗?我什至无法在浏览器中导航到那里
    • 是的,如果它是一个“.in”站点,您可能必须使用 VPN。
    猜你喜欢
    • 2015-07-30
    • 2013-10-03
    • 2021-09-02
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-03
    相关资源
    最近更新 更多