本文介绍了调试时代码工作正常,但它在没有调试的情况下运行时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 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,条目); } } HtmlElementCollection filterCollection = webBrowser1.Document.GetElementById( filterSortBy)。GetElementsByTagName( option); 列表< HtmlElement> filterList = new List< HtmlElement>(); foreach (HtmlElement filterItem in filterCollection){filterList.Add(filterItem); } HtmlElement filterElement = (HtmlElement)filterList.Where(filterOption = > filterOption.GetAttribute( value)。等于( 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( 点击); } } while (webBrowser1.ReadyState!= WebBrowserReadyState.Complete) {申请表。的DoEvents(); } MessageBox.Show( 已加载); System.Timers.Timer myTimer = new System.Timers.Timer( 5000 ); myTimer.Enabled = true ; myTimer.Start(); myTimer.Stop(); if (webBrowser1.ReadyState == WebBrowserReadyState.Complete) // 从此处开始,代码无效。 { HtmlElementCollection avCollection = webBrowser1.Document.GetElementById( filterAvailability)。GetElementsByTagName( 选项); 列表< HtmlElement> avList = new 列表< HtmlElement>(); foreach (HtmlElement avItem in avCollection) { avList。加入(avItem); } HtmlElement avElement = (HtmlElement)avList.Where(avOption = > avOption.GetAttribute( value)。等于( 1))。SingleOrDefault(); if (avElement.GetAttribute( value)。Equals( 1)) { avElement.SetAttribute( Selected, 1); avElement.InvokeMember( 点击); } bookCollection = webBrowser1.Document.GetElementsByTagName( button); foreach (HtmlElement curElement in bookCollection) { if (curElement.GetAttribute( id) .Equals( searchSubmit)) { curElement.InvokeMember( click); } } } 这里是整个代码。我尝试用计时器思考设置延迟,因为它反应太快但计时器也没有工作所以我不确定问题是什么。 编辑: 无效的代码假设创建具有3个计数的avList并选择值1.值1表示网站的库存可用性。 在没有调试的情况下运行时似乎完全忽略了if条件中写的代码。 谢谢解决方案 我对这个解决方案不太确定。 在Web.Config文件中,如果你看一下Web.Config类别下会有两个文件。 子该分支的文件将命名为 1.Web.Debug.config 2.Web.Release.config 如果您正在调试文件,将从文件 .Web.Debug.config 如果您执行文件,则有机会使用来自常见Web.config或其他文件的文件。 请检查配置条目是否正常和相同。 请确认您的观察,以便我们为您提供帮助。还要符合您所面临的错误类型或未按预期工作的功能。这将有助于其他人指导你。 谢谢! 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"); } } }here is the whole code. I tried setting the delay with timer thinking because it is reacting too fast but the timer is also not working so im not sure what the problem could be.EDIT:the code that doesnt work is suppose to create the avList with 3 counts and choose the value 1. the value 1 represents the In Stock availability of the website.when running without debugging it seems to completely ignore the code written in the if condition.thanks 解决方案 Hi,I am not so sure about this solution.In Web.Config File, if you take a look there will be two Files under the "Web.Config" category.The sub Files of that Branch will be named as 1.Web.Debug.config2.Web.Release.configIf you are Debugging the File, the Config details will be consumed from the File .Web.Debug.config and if you execute the File there is a chance to use the file from common Web.config or the other.Please check the configure entries are fine and identitical.Please confirm your observation, so we can help you in this. Also conform what type of Error you are facing or what functionality not working as expected. That will be helpfull for others to guide you.Thanks! 这篇关于调试时代码工作正常,但它在没有调试的情况下运行时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-11 21:17