如何获得code模拟网页按钮后点击来源$ C ​​$ C [英] How to get source code after simulate web page button click in code

查看:139
本文介绍了如何获得code模拟网页按钮后点击来源$ C ​​$ C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一个网页的源代码code,我点击按钮后得到的。

I am trying to get a source code of a web page which I get after click on a button.

我可以点击位于网页上的按钮。

I am able to click a button located on a web page.

 webBrowser1.Navigate(url);
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
    Application.DoEvents();
}               
webBrowser1.Document.GetElementById("downloadButton").InvokeMember("click");

现在在这之后一个新的窗口出现。这是可能得到这个新窗口的来源$ C ​​$ C点击后出现。

Now after this a new window appear. Is this possible to get the source code of this new window appear after the click.

推荐答案

一个的哈克的办法是:


  1. 附加一个事件处理程序的按钮的onclick事件。

  2. 然后,一旦该事件被触发时,使用的 Microsoft Internet控制(SHDOCVW)的类型库,以获得在IE中打开过的网址。

  3. 最后,导航到网址,一旦文件被加载,您可以通过 webBrowser1.DocumentText 属性文件的来源。

  1. Attach an event handler to the 'onclick' event of the button.
  2. Then, once the event is triggered, use the Microsoft Internet Controls (SHDocVw) type library in order to get the last URL opened in IE.
  3. Lastly, navigate to the URL and once the document is loaded, get the source of the document from the webBrowser1.DocumentText property.

在你的项目中,添加到的 Microsoft Internet控制的参考的类型库(你会在 COM 的标签找到它)。加入你的文件的顶部:

In your project, add a reference to the Microsoft Internet Controls type library (you'll find it in the COM tab). Add at the top of your file:

using SHDocVw;

在code:

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");

这篇关于如何获得code模拟网页按钮后点击来源$ C ​​$ C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆