WebBrowser帮助-导航到url,等待一段时间,然后单击按钮
本文关键字:等待 一段时间 单击 按钮 然后 url 帮助 导航 WebBrowser | 更新日期: 2023-09-27 18:20:03
我需要从网站获得下载链接。要得到它,我需要等待10秒,直到它可以点击。是否可以使用WebBrowser()获取下载链接?
这是按钮的来源。
<input type="submit" id="btn_download" class="btn btn-primary txt-bold" value="Download File">
这是我尝试过的:
WebBrowser wb = new WebBrowser();
wb.ScriptErrorsSuppressed = true;
wb.AllowNavigation = true;
wb.Navigate(url);
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
while (wb.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
Thread.Sleep(10000);
HtmlElement element = wb.Document.GetElementById("btn_download");
element.InvokeMember("submit");
while (wb.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
string x = wb.Url.ToString();
这里怎么了?
编辑-尝试了这个,但仍然不起作用-顺便说一句,我觉得我把代码搞砸了,我不知道:)
public void WebBrowser()
{
WebBrowser wb = new WebBrowser();
wb.ScriptErrorsSuppressed = true;
wb.AllowNavigation = true;
wb.Navigate(URL);
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
while (wb.ReadyState != WebBrowserReadyState.Complete)
Application.DoEvents();
wb.Dispose();
}
public void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser wb = (WebBrowser)sender;
string x = wb.Url.ToString();
if (!x.Contains("server")) // download link must conatin server
{
System.Timers.Timer t = new System.Timers.Timer();
t.Interval = 10000;
t.AutoReset = true;
t.Elapsed += new ElapsedEventHandler(TimerElapsed);
t.Start();
HtmlElement element = wb.Document.GetElementById("btn_download");
element.InvokeMember("Click");
while (wb.ReadyState != WebBrowserReadyState.Complete)
Application.DoEvents();
}
else
MessageBox.Show(x);
}
public void TimerElapsed(object sender, ElapsedEventArgs e)
{
Application.DoEvents();
}
我对回答这个问题感到不舒服,但希望这能展示如何以不同的方式进行;
a) 你不需要使用浏览器控件来下载网页
b) 你不需要这些繁忙的等待来等待什么。。
c) 它可能有助于显示HttpClient、HtmlAgilityPack+async/await的使用
现在,写一个类似下面的方法
async Task<string> DownloadLink(string linkID)
{
string url = "http://sfshare.se/" + linkID;
using (var clientHandler = new HttpClientHandler() { CookieContainer = new CookieContainer(), AllowAutoRedirect = false })
{
using (HttpClient client = new HttpClient(clientHandler))
{
//download html
var html = await client.GetStringAsync(url).ConfigureAwait(false);
//Parse it
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
var inputs = doc.DocumentNode.SelectNodes("//input[@type='hidden']")
.ToDictionary(i => i.Attributes["name"].Value, i => i.Attributes["value"].Value);
inputs["referer"] = url;
//Wait 10 seconds
await Task.Delay(1000 * 10).ConfigureAwait(false);
//Click :) Send the hidden inputs. op=download2&id=ssmwvrxx815l......
var response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Post, url) { Content = new FormUrlEncodedContent(inputs) })
.ConfigureAwait(false);
//Get the download url
var downloadUri = response.Headers.Location.ToString();
var localFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), Path.GetFileName(downloadUri));
//Download
using (var file = File.Create(localFileName))
{
var stream = await client.GetStreamAsync(downloadUri).ConfigureAwait(false); ;
await stream.CopyToAsync(file).ConfigureAwait(false);
}
return localFileName;
}
}
}
并在标记为async的方法中调用它,如下所示
async void Test()
{
var downloadedFile = await DownloadLink("vydjxq40g503");
}
PS:这个答案需要HtmlAgilityPack来解析返回的html。