Webbrowser completed 不显示所有元素
Webbrowser completed doesn't show all elements
我有一个正在加载网站的网络浏览器。如果网站触发 "completed" 事件,我的 HTMLElementCollection 会显示计数“179”。如果我单击按钮并调用相同的方法来获取所有 HTMLElements,它会告诉我计数为“194”。问题是:如果 "completed"-Event 正在触发,一些 HTMLElements 没有加载,需要更长的时间,我需要点击它的 HTMLElement 也丢失了。
用代码来解释:
private void Webbrowser_DocumentComplete(object pDisp, ref object URL)
{
if (URL.ToString() == "testsite")
{
HtmlElementCollection c1 = webBrowser1.Document.GetElementsByTagName("div");
foreach (HtmlElement e2 in c1)
{
if (e2.GetAttribute("classname") == "btn3")
{
if (e2.InnerText == "follow")
{
e2.InvokeMember("click");
}
}
}
}
}
"c1" 的计数是 179。
如果我等待 1-2 秒,然后单击具有相同代码的按钮,例如:
private void Button1_Click(object sender, EventArgs e)
{
HtmlElementCollection c1 = webBrowser1.Document.GetElementsByTagName("div");
foreach (HtmlElement e2 in c1)
{
if (e2.GetAttribute("classname") == "btn3")
{
if (e2.InnerText == "follow")
{
e2.InvokeMember("click");
}
}
}
}
"c1" 的计数是 194。
问题是:如果页面构建完成,我如何等待几秒钟?
我需要计数 194,因为有一个 HTMLElement 我想点击它!
您可以使用计时器来处理这个问题。完成以下功能等待几秒钟。
public void WaitForSecond(int min)
{
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
if (min == 0 || min < 0) return;
timer1.Interval = min * 1000;
timer1.Enabled = true;
timer1.Start();
timer1.Tick += (s, e) =>
{
timer1.Enabled = false;
timer1.Stop();
};
while (timer1.Enabled)
{
Application.DoEvents();
}
}
此外,您可以通过 LINQ
减少代码
HtmlElement e2= (from HtmlElement element in
webBrowser1.Document.GetElementsByTagName("div")
select element)
.Where(x => x.GetAttribute("classname") != null
&& x.GetAttribute("classname") =="btn3"
&& x.InnerText != null
&& x.InnerText == "follow").FirstOrDefault();
首先,检查您的 HTML 元素是否已加载,否则请等待。如果加载了 HTML 元素,则处理进一步的步骤
HtmlElement e2= null;
int cnt = 0;
do
{
WaitForSecond(1);
cnt++;
if (cnt > 60)
{
MessageBox.Show("Web page not loaded");
break;
}
e2= (from HtmlElement element in webBrowser1.Document.GetElementsByTagName("div") select element)
.Where(x => x.GetAttribute("classname") != null
&& x.GetAttribute("classname") =="btn3"
&& x.InnerText != null
&& x.InnerText == "follow").FirstOrDefault();
} while e2 == null);
谢谢大家,但我 google 再次找到了一个非常好的(对我的工作而言)解决方法。
对于所有人:我希望在导航到页面后,计时器(或其他东西)等待 x 秒,在计时器等待期间,页面正在加载,之后,我想点击按钮。
首先:代码不是我做的。来自 https://dotnet-snippets.de/snippet/webbrowser-navigate-and-wait-seconds/15171
首先在开头声明:
public delegate void NavigateDoneEvent();
public static event NavigateDoneEvent Done;
private static System.Windows.Forms.Timer wait;
您不需要将其用作静态。
之后你需要创建这个函数
public static void Wait(WebBrowser Browser, string Url, double Seconds)
{
Browser.Navigate(Url);
wait = new System.Windows.Forms.Timer();
wait.Interval = Convert.ToInt32(Seconds * 1000);
wait.Tick += (s, args) =>
{
if (Done != null) Done();
wait.Enabled = false;
};
wait.Enabled = true;
}
你可以这样调用这个函数:
Wait(webBrowser1, "somesite.com", 20);
Done += afterWaitDoSomething;
我有一个正在加载网站的网络浏览器。如果网站触发 "completed" 事件,我的 HTMLElementCollection 会显示计数“179”。如果我单击按钮并调用相同的方法来获取所有 HTMLElements,它会告诉我计数为“194”。问题是:如果 "completed"-Event 正在触发,一些 HTMLElements 没有加载,需要更长的时间,我需要点击它的 HTMLElement 也丢失了。
用代码来解释:
private void Webbrowser_DocumentComplete(object pDisp, ref object URL)
{
if (URL.ToString() == "testsite")
{
HtmlElementCollection c1 = webBrowser1.Document.GetElementsByTagName("div");
foreach (HtmlElement e2 in c1)
{
if (e2.GetAttribute("classname") == "btn3")
{
if (e2.InnerText == "follow")
{
e2.InvokeMember("click");
}
}
}
}
}
"c1" 的计数是 179。
如果我等待 1-2 秒,然后单击具有相同代码的按钮,例如:
private void Button1_Click(object sender, EventArgs e)
{
HtmlElementCollection c1 = webBrowser1.Document.GetElementsByTagName("div");
foreach (HtmlElement e2 in c1)
{
if (e2.GetAttribute("classname") == "btn3")
{
if (e2.InnerText == "follow")
{
e2.InvokeMember("click");
}
}
}
}
"c1" 的计数是 194。
问题是:如果页面构建完成,我如何等待几秒钟? 我需要计数 194,因为有一个 HTMLElement 我想点击它!
您可以使用计时器来处理这个问题。完成以下功能等待几秒钟。
public void WaitForSecond(int min)
{
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
if (min == 0 || min < 0) return;
timer1.Interval = min * 1000;
timer1.Enabled = true;
timer1.Start();
timer1.Tick += (s, e) =>
{
timer1.Enabled = false;
timer1.Stop();
};
while (timer1.Enabled)
{
Application.DoEvents();
}
}
此外,您可以通过 LINQ
减少代码HtmlElement e2= (from HtmlElement element in
webBrowser1.Document.GetElementsByTagName("div")
select element)
.Where(x => x.GetAttribute("classname") != null
&& x.GetAttribute("classname") =="btn3"
&& x.InnerText != null
&& x.InnerText == "follow").FirstOrDefault();
首先,检查您的 HTML 元素是否已加载,否则请等待。如果加载了 HTML 元素,则处理进一步的步骤
HtmlElement e2= null;
int cnt = 0;
do
{
WaitForSecond(1);
cnt++;
if (cnt > 60)
{
MessageBox.Show("Web page not loaded");
break;
}
e2= (from HtmlElement element in webBrowser1.Document.GetElementsByTagName("div") select element)
.Where(x => x.GetAttribute("classname") != null
&& x.GetAttribute("classname") =="btn3"
&& x.InnerText != null
&& x.InnerText == "follow").FirstOrDefault();
} while e2 == null);
谢谢大家,但我 google 再次找到了一个非常好的(对我的工作而言)解决方法。 对于所有人:我希望在导航到页面后,计时器(或其他东西)等待 x 秒,在计时器等待期间,页面正在加载,之后,我想点击按钮。
首先:代码不是我做的。来自 https://dotnet-snippets.de/snippet/webbrowser-navigate-and-wait-seconds/15171
首先在开头声明:
public delegate void NavigateDoneEvent();
public static event NavigateDoneEvent Done;
private static System.Windows.Forms.Timer wait;
您不需要将其用作静态。
之后你需要创建这个函数
public static void Wait(WebBrowser Browser, string Url, double Seconds)
{
Browser.Navigate(Url);
wait = new System.Windows.Forms.Timer();
wait.Interval = Convert.ToInt32(Seconds * 1000);
wait.Tick += (s, args) =>
{
if (Done != null) Done();
wait.Enabled = false;
};
wait.Enabled = true;
}
你可以这样调用这个函数:
Wait(webBrowser1, "somesite.com", 20);
Done += afterWaitDoSomething;