string entry = Titleentry.Text;
webBrowser1.Navigate("http://www.bookdepository.com/search/advanced");
//HtmlElementCollection bookCollection;
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
HtmlElementCollection bookCollection = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement curElement in bookCollection)
{
if ((curElement.GetAttribute("id").ToString() == "searchTitle"))
{
curElement.SetAttribute("value", entry);
}
}
HtmlElementCollection filterCollection = webBrowser1.Document.GetElementById("filterSortBy").GetElementsByTagName("option");
List<HtmlElement> filterList = new List<HtmlElement>();
foreach (HtmlElement filterItem in filterCollection) { filterList.Add(filterItem); }
HtmlElement filterElement =
(HtmlElement)filterList.Where(filterOption => filterOption.GetAttribute("value").Equals("price_low_high", StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault();
if (filterElement.GetAttribute("value").Equals("price_low_high"))
{
filterElement.SetAttribute("Selected", "price_low_high");
filterElement.InvokeMember("click");
}
bookCollection = webBrowser1.Document.GetElementsByTagName("button");
foreach (HtmlElement curElement in bookCollection)
{
if (curElement.GetAttribute("id").Equals("searchSubmit"))
{
curElement.InvokeMember("click");
}
}
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
MessageBox.Show("Loaded");
System.Timers.Timer myTimer = new System.Timers.Timer(5000);
myTimer.Enabled = true;
myTimer.Start();
myTimer.Stop();
if (webBrowser1.ReadyState == WebBrowserReadyState.Complete) //from here on the code doesnt work.
{
HtmlElementCollection avCollection = webBrowser1.Document.GetElementById("filterAvailability").GetElementsByTagName("option");
List<HtmlElement> avList = new List<HtmlElement>();
foreach(HtmlElement avItem in avCollection)
{
avList.Add(avItem);
}
HtmlElement avElement =
(HtmlElement)avList.Where(avOption => avOption.GetAttribute("value").Equals("1")).SingleOrDefault();
if (avElement.GetAttribute("value").Equals("1"))
{
avElement.SetAttribute("Selected", "1");
avElement.InvokeMember("click");
}
bookCollection = webBrowser1.Document.GetElementsByTagName("button");
foreach (HtmlElement curElement in bookCollection)
{
if (curElement.GetAttribute("id").Equals("searchSubmit"))
{
curElement.InvokeMember("click");
}
}
}
這裏是整個代碼。我試着用定時器思考設置延遲,因爲它反應速度太快,但計時器也沒有工作,所以我不知道問題可能是什麼。該代碼在調試時工作正常,但在不調試時運行時不起作用
是不工作的代碼是假設創建3個計數avList並選擇值1的值爲1表示該網站的庫存可用性。
沒有調試運行時,它似乎完全忽視寫在if條件的代碼。
謝謝
請謹慎使用webBrowser1.ReadyState!= WebBrowserReadyState.Complete while語句。雖然在這種情況下不太可能發生,但像這樣的代碼可能會創建一個無限循環,因爲條件可能永遠不會完成,例如保持未初始化 –
我看到。但我認爲那不太可能發生 –
if語句中的WebBrowserReadyState值是什麼?從我所能告訴的狀態改變到跳出while循環所需的時間。 –