我正在尝试获取单击按钮后获得的网页源代码。
我可以点击网页上的按钮。
webBrowser1.Navigate(url);
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
webBrowser1.Document.GetElementById("downloadButton").InvokeMember("click");
此后会出现一个新窗口。这可以在单击后显示此新窗口的源代码。
答案 0 :(得分:0)
hacky 方法是:
webBrowser1.DocumentText
属性获取文档的来源。在您的项目中,添加对 Microsoft Internet Controls 类型库的引用(您可以在 COM 选项卡中找到它)。在文件顶部添加:
using SHDocVw;
代码:
webBrowser1.Navigate(url);
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
// assign the button to a variable
var button = webBrowser1.Document.GetElementById("downloadButton");
// attach an event handler for the 'onclick' event of the button
button.AttachEventHandler("onclick", (a, b) =>
{
// use the Microsoft Internet Controls COM library
var shellWindows = new SHDocVw.ShellWindows();
// get the location of the last window in the collection
var newLocation = shellWindows.Cast<SHDocVw.InternetExplorer>()
.Last().LocationURL;
// navigate to the newLocation
webBrowser1.Navigate(newLocation);
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
// get the document's source
var source = webBrowser1.DocumentText;
});
button.InvokeMember("click");