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 表示网站的 In Stock 可用性。
在没有调试的情况下运行时,它似乎完全忽略了 if 条件中编写的代码。
谢谢