如何在c中设置html元素的值

本文关键字:html 元素 设置 | 更新日期: 2023-09-27 17:59:47

我正在开发一个从网站获取数据的项目。所以我使用了webBrowser并将url设置为travelchi.ir,我在下面写了代码

 private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            HtmlDocument doc = webBrowser1.Document;
            HtmlElement Source = doc.GetElementById("from");
            HtmlElement Destination = doc.GetElementById("to");
            HtmlElement adultCount = doc.GetElementById("adultCount");
            HtmlElement childCount = doc.GetElementById("childCount");
            HtmlElement infantCount = doc.GetElementById("infantCount");
            var links = webBrowser1.Document.GetElementsByTagName("button");
            var inputs = webBrowser1.Document.GetElementsByTagName("input");
            Source.SetAttribute("value", "شیراز");
            Destination.SetAttribute("value", "تهران");
            adultCount.SetAttribute("value", "1 بزرگسال");
            childCount.SetAttribute("value", "1 کودک");
            infantCount.SetAttribute("value", "0 خردسال");
            foreach (HtmlElement input in inputs)
            {
                if (input.GetAttribute("className") == "form-control")
                {
                    input.SetAttribute("value", "1395/05/14");
                }
            }
            foreach (HtmlElement link in links)
            {
                if (link.GetAttribute("className") == "btn btn-primary")
                {
                    link.InvokeMember("click");
                }
            } 

要获取元素并为其设置值,所以在最后我发现此代码无法设置值,请帮助我解决

如何在c中设置html元素的值

方法是getElementById而不是getElementById

getElementsByTagName和setAttribute 如下

参考:-

  1. https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById
  2. https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByTagName
  3. https://developer.mozilla.org/en/docs/Web/API/Element/setAttribute