我正在开发一个从网站上获取数据的项目。所以我使用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");
}
}
到getelement并设置值,所以最后我知道这段代码不能设置值请帮我解决一下
答案 0 :(得分:1)
方法 getElementById 而非 GetElementById 。
对于getElementsByTagName和setAttribute
也是如此参考: -