C# get element by name

前端 未结 3 1717
挽巷
挽巷 2020-12-20 20:19

Soo ive figured out how to get element by id, but i dont know how i can get element by name Here is my code:

private void SendData()
{
    webBrowser1.Docume         


        
相关标签:
3条回答
  • 2020-12-20 20:40

    You can get an HtmlElementCollection - for example, using GetElementsByTagName method. Then, HtmlElementCollection has GetElementsByName method:

    webBrowser1.Document
        .GetElementsByTagName("input")
        .GetElementsByName("textfield2")[0]
            .SetAttribute("value", textBox1.Text);
    
    0 讨论(0)
  • 2020-12-20 20:41

    You can use HtmlElementCollection.GetElementsByName to take the value of the elements

    webBrowser1.Document.GetElementsByName("textfield2").SetAttribute("value", textBox1.Text);
    

    EDIT

    foreach (HtmlElement he in webBrowser1.Document.All.GetElementsByName("textfield2"))
    {
        he.SetAttribute("value", textBox1.Text);
    }
    
    0 讨论(0)
  • 2020-12-20 20:48

    You can't access the elements directly by name, but you could access it by finding the input tags first, and indexing into the result to find the tags by name.

    webBrowser1.Document.GetElementsByTagName("input")["textfield2"]
    

    or

    webBrowser1.Document
        .GetElementsByTagName("input")
        .GetElementsByName("textfield2")[0]
    
    0 讨论(0)
提交回复
热议问题