简体   繁体   English

等待网页加载到Webbrowser C#中

[英]Wait until page loads in webbrowser C#

I have a program that testing the web page - clicking on the web elements. 我有一个测试网页的程序-单击Web元素。 I'm using the C# WebBrowswer control and I have a problem before inserting the text in the text field, I want to wait for the page to load. 我正在使用C#WebBrowswer控件,但是在将文本插入文本字段之前遇到问题,我想等待页面加载。 for wait for the page to load, I'm using : 为了等待页面加载,我正在使用:

  while (true) { System.Windows.Forms.Application.DoEvents(); foreach (HtmlElement el in web_Browser.Document.GetElementsByTagName("р")) { if (el.GetAttribute("title") == "Custom") != null) { break; } } } 

but I get an error in the line: var elmC = web_Browser.Document.GetElementsByTagName("input"); 但我在一行中得到一个错误:var elmC = web_Browser.Document.GetElementsByTagName(“ input”); "unreachable code detected" “检测到无法访问的代码”

  public void GetStatus(string innerText) { try { while (true) { System.Windows.Forms.Application.DoEvents(); foreach (HtmlElement el in web_Browser.Document.GetElementsByTagName("р")) { if (el.GetAttribute("title") == "Custom") != null) { break; } } } var elmC = web_Browser.Document.GetElementsByTagName("input"); foreach (HtmlElement elm in elmC) { if (elm.Id == "num") { elm.InnerText = String.Empty; Wait(1); elm.InnerText = innerText; } } } catch (Exception ex) { MessageBox.Show("Error " + ex.Message); } } 

Your break; 你休息一下 statement is only going to break out of the foreach loop, not the while(true) loop, so that loop will run forever, any code after is unreachable... 语句只会中断foreach循环,而不会影响while(true)循环,因此该循环将永远运行,之后的任何代码都无法访问...

Maybe try something like : 也许尝试像:

var run = true; var run = true;

 while (run)
                {
                    System.Windows.Forms.Application.DoEvents();
                    foreach (HtmlElement el in web_Browser.Document.GetElementsByTagName("р"))
                    {
                        if (el.GetAttribute("title") == "Custom") != null)
                        {
                           run = false;
                        }
                    }
                }

But usually any code that reads " while() { // check async operation } " is generally bad ... Consider using an event or Thread.sleep() perhaps 但是通常任何读取“ while(){//检查异步操作}”的代码通常都是不好的...考虑使用事件或Thread.sleep()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM