所以我想出了如何通过 id 获取元素,但我不知道如何通过名称获取元素。
private void SendData()
{
webBrowser1.Document.GetElementById("textfield1")
.SetAttribute("value", textBox1.Text);
webBrowser1.Document.GetElementById("textfield2")
.SetAttribute("value", textBox1.Text);
}
问题在于 HTML textfield1
是一个 id 而 textfield2
是一个名称。所以我想弄清楚如何获取 textfield2
:
<html>
<input type="text" id="textfield1" value="TEXT1"><br>
<input type="text" name="textfield2" value="TEXT2"><br>
<input type="submit" value="Submit">
</html>
最佳答案
您可以获得 HtmlElementCollection
- 例如,使用 GetElementsByTagName
方法。然后,HtmlElementCollection
有 GetElementsByName
方法:
webBrowser1.Document
.GetElementsByTagName("input")
.GetElementsByName("textfield2")[0]
.SetAttribute("value", textBox1.Text);
关于C# 根据名称获取元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32904235/